本文翻译自: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? 任何人都可以提供参考原因吗?
#1楼
参考:https://stackoom.com/question/IEPp/如何在SQL-Server中查询大于特定日期的所有日期
#2楼
Try enclosing your date into a character string. 尝试将日期括在字符串中。
select *
from dbo.March2010 A
where A.Date >= '2010-04-01';
#3楼
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
更不易读。
#4楼
DateTime start1 = DateTime.Parse(txtDate.Text);
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= start1;
First convert TexBox into the Datetime then....use that variable into the Query 首先将TexBox转换为Datetime然后....将该变量用于Query
#5楼
We can use like below as well 我们也可以使用如下
SELECT *
FROM dbo.March2010 A
WHERE CAST(A.Date AS Date) >= '2017-03-22';
SELECT *
FROM dbo.March2010 A
WHERE CAST(A.Date AS Datetime) >= '2017-03-22 06:49:53.840';
#6楼
To sum it all up, the correct answer is..... 总而言之,正确的答案是......
select * from db where Date >= '20100401' (Format of date yyyymmdd) select * from db where Date> ='20100401'(日期格式yyyymmdd)
This will avoid any problem with other language systems and will use the index 这将避免与其他语言系统的任何问题,并将使用索引