本文翻译自: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 这将避免与其他语言系统的任何问题,并将使用索引
本文探讨了在SQL Server中查询特定日期之后所有记录的有效方法。针对日期字段格式问题,提供了多种解决方案,包括使用字符串格式的日期、转换函数以及CAST操作。通过实际案例,展示了如何避免因日期格式错误导致的问题。
5338

被折叠的 条评论
为什么被折叠?



