Comparing dates in Sql
I am having trouble trying to compare dates in my sql query:
SELECT restrictions, restric开发者_如何转开发tions_adddate FROM restrictions WHERE id = '555555'
AND restrictions_adddate BETWEEN ? and ?;
Both of the parameters system print in the format 'mm-dd-yyyy'. I am getting a wrong date format error, but have tried doing it in multiple ways. Any help is appreciated, and if more info is needed, please let me know. thanks.
This should work on any standard compliant DBMS:
SELECT restrictions, restrictions_adddate
FROM restrictions
WHERE id = '555555'
AND restrictions_adddate BETWEEN DATE '2011-01-01' and DATE '2011-12-31';
This is my preference:
AND restrictions_adddate BETWEEN
CONVERT(datetime, FLOOR(CONVERT(float, CAST('01-01-1900' AS DATETIME))))
AND CONVERT(datetime, FLOOR(CONVERT(float, Getdate())));
This allows you to compare dates (minus the time stamps).
Try checking out the CONVERT
function T-SQL reference in BOL. You should be able to use this dictate the format of your dates.
精彩评论