简体   繁体   English

比较 Sql 中的日期

[英]Comparing dates in Sql

I am having trouble trying to compare dates in my sql query:我在尝试比较 sql 查询中的日期时遇到问题:

SELECT restrictions, restrictions_adddate FROM restrictions WHERE id = '555555'
AND restrictions_adddate BETWEEN ? and ?;

Both of the parameters system print in the format 'mm-dd-yyyy'.两个参数系统都以“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:这应该适用于任何符合标准的 DBMS:

SELECT restrictions, restrictions_adddate 
FROM restrictions 
WHERE id = '555555'
  AND restrictions_adddate BETWEEN DATE '2011-01-01' and DATE '2011-12-31';

Try checking out the CONVERT function T-SQL reference in BOL.尝试查看 BOL 中的CONVERT function T-SQL 参考。 You should be able to use this dictate the format of your dates.您应该能够使用它来指定日期的格式。

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).这允许您比较日期(减去时间戳)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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