Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

sql - 如何在SQL Server中查询大于特定日期的所有日期?(How do I query for all dates greater than a certain date in SQL Server?)

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

(A.Date看起来像: 2010-03-04 00:00:00.000)

However, this is not working.

(但是,这不起作用。)

Can anyone provide a reference for why?

(任何人都可以提供参考原因吗?)

  ask by Eric Francis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

(在您的查询中, 2010-4-01被视为数学表达式,因此本质上它是读取的)

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.)

(( 2010 minus 4 minus 1 is 2005将其转换为正确的datetime ,并使用单引号将解决此问题。))

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.

(它会为你做转换,但在我看来,它比你明确转换为维护程序员的DateTime更不易读。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...