在SQL Server中如何查询大于某个日期的所有日期? [英] How do I query for all dates greater than a certain date in SQL Server?
问题描述
我正在尝试:
SELECT *
FROM dbo.March2010 A
WHERE A. Date> = 2010-04-01;
A.Date
看起来像: 2010-03-04 00:00:00.000
但是,这不工作。任何人都可以提供参考为什么?
select *
from dbo 。2010年3月A
其中A.Date> =转换(datetime,'2010-04-01')
在您的查询中, 2010-4-01
被视为数学表达式,因此本质上它读取
$ pre>
select *
from dbo.March2010 A
其中A.Date> = 2005;
( 2010减去4减1是2005
将它转换为正确的 datetime
,并使用单引号将修复此问题。)
,解析器可能会让你离开
select *
from dbo.March2010 A
where A.Date> ='2010-04-01'
它会为您进行转换,但在我看来,对于将在你后面的维护程序员明确转换为 DateTime
可读性
I'm trying:
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= 2010-04-01;
A.Date
looks like: 2010-03-04 00:00:00.000
However, this is not working. Can anyone provide a reference for why?
select *
from dbo.March2010 A
where A.Date >= Convert(datetime, '2010-04-01' )
In your query, 2010-4-01
is treated as a mathematical expression, so in essence it read
select *
from dbo.March2010 A
where A.Date >= 2005;
(2010 minus 4 minus 1 is 2005
Converting it to a proper datetime
, and using single quotes will fix this issue.)
Technically, the parser might allow you to get away with
select *
from dbo.March2010 A
where A.Date >= '2010-04-01'
it will do the conversion for you, but in my opinion it is less readable than explicitly converting to a DateTime
for the maintenance programmer that will come after you.
这篇关于在SQL Server中如何查询大于某个日期的所有日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!