[英]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?任何人都可以提供原因的参考吗?
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
可读性。
Try enclosing your date into a character string.尝试将您的日期包含在一个字符串中。
select *
from dbo.March2010 A
where A.Date >= '2010-04-01';
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';
To sum it all up, the correct answer is :总结一下,正确答案是:
select * from db where Date >= '20100401' (Format of date yyyymmdd)
This will avoid any problem with other language systems and will use the index.这将避免其他语言系统的任何问题,并将使用索引。
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 然后....将该变量用于查询
The date format has no issue with me(Mydate's data_type is datetime) :日期格式对我没有问题(Mydate 的 data_type 是 datetime):
Where Mydate>'10/25/2021' or Where Mydate>'2021-10-25'我的日期>'10/25/2021' 或我的日期>'2021-10-25'
but if add a time, above answers are not working.但如果添加时间,上述答案不起作用。
Here is what I do:这是我所做的:
where cast(Mydate as time)>'22:00:00' where cast(Mydate as time)>'22:00:00'
If your query needs a date, please add date such as:如果您的查询需要日期,请添加日期,例如:
where cast(Mydate as time)>'22:00:00' and Mydate='10/25/2021' where cast(Mydate as time)>'22:00:00' and Mydate='10/25/2021'
In your query you didn't use single quote around date.在您的查询中,您没有在日期周围使用单引号。 That was the problem.那就是问题所在。 However, you can use any of the following query to compare date但是,您可以使用以下任何查询来比较日期
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= '2010-04-01';
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= CAST('2010-04-01' as Date);
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= Convert(datetime, '2010-04-01' )
First you need to convert both the dates in same format before conversion首先,您需要在转换前以相同的格式转换两个日期
SELECT *
FROM dbo.March2010 A
WHERE CONVERT(DATE, A.Date) >= 2010-04-01;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.