SQL错误的比较日期

问题描述 投票:0回答:1

我需要数据,其中code_filter以84开头,Issuing_country为AL,并且End_date_of_validity_updated小于今天的日期(即到期),但是具有以下代码:

SELECT DISTINCT (code_filter) 
FROM MyTable
WHERE (code_filter LIKE '84%' AND Issuing_country = 'AL')  
  AND End_date_of_validity_updated < '06-05-2020 23:59:59.999' 

我得到这个结果:

enter image description here

我的意思是第一个可以,但是为什么第二个过滤了,尽管日期没有过期?

提前感谢!

sql-server datetime compare
1个回答
0
投票

尝试

SELECT DISTINCT (code_filter) FROM MyTable

where (code_filter like'84%' and Issuing_country = 'AL')  and 

End_date_of_validity_updated < cast(getdate() as date)

也许您从字符串中提取日期失败您也可以尝试进行显式转换

End_date_of_validity_updated < cast('06-05-2020 23:59:59.999' as date)
© www.soinside.com 2019 - 2024. All rights reserved.