简体   繁体   中英

Where clause fails if using same date in a “greater than or equal to” and a “less than or equal to”

Why does this query not return any rows when date1 , date2 , and dateField are the same and there are rows where dateField == date1 that should be returned?

Examples:

select * from tableName where dateField >= date1 and dateField <= date2

It appears that the time part of the smalldatetime field dateField used to be all zeros, and now it's not. The solution was to just compare the date portion of dateField .

SELECT * 
FROM tableName
WHERE 
CONVERT(DATE, dateField) >= date1 AND CONVERT(DATE, dateField) <= date2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM