[英]SQL Server datetime comparison in different formats
I have a SQL query in which I will be passing dd/mm/yyyy
but the SQL query requires mm/dd/yyyy
. 我有一个SQL查询,将在其中传递
dd/mm/yyyy
但该SQL查询需要mm/dd/yyyy
。
How can I enable this query to take dd/mm/yyyy
and display the correct result based on the correct format? 如何启用此查询以
dd/mm/yyyy
并根据正确的格式显示正确的结果? What is the best way to achieve this thing in SQL Server 2008? 在SQL Server 2008中实现此目标的最佳方法是什么?
SELECT int_ExchangeRateId, int_LocationId, dtm_Date
FROM ExchangeRate
WHERE dtm_Date >= '01/02/2006' AND dtm_Date <= '12/02/2006' AND int_LocationId = 98
THIS WORKS CORRECTLY: 这项工作正确地:
SELECT int_ExchangeRateId, int_LocationId,dtm_Date
FROM ExchangeRate
WHERE CAST(dtm_Date AS DATE) BETWEEN '2006-02-02' AND '2006-02-12'
AND int_LocationId=98
For the query I would use dates in YYYY-MM-DD
format. 对于查询,我将使用
YYYY-MM-DD
格式的日期。 As to the output see kalyan's answer. 关于输出,请参见kalyan的答案。
EDIT: To summarise the comments below (thanks to marc_s) it is necessary to use the format YYYYMMDD
when the time part is not included unless you are using the new date types date
, datetime2
and datetimeoffset
in SQL Server 2008. 编辑:要总结以下注释(感谢marc_s), 除非您在SQL Server 2008中使用新的日期类型
date
, datetime2
和datetimeoffset
, 否则不包括时间部分时,必须使用格式YYYYMMDD
。
为此,请参考DateTime格式,通过提供某些参数,我们可以将日期格式化为所需格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.