How do I query for all dates greater than a certain date in SQL Server?(如何在 SQL Server 中查询大于某个日期的所有日期?)
问题描述
我正在尝试:
SELECT *
FROM dbo.March2010 A
WHERE A.Date >= 2010-04-01;
A.Date 看起来像:2010-03-04 00:00:00.000
然而,这是行不通的.
任何人都可以提供参考原因吗?
Can anyone provide a reference for why?
推荐答案
select *
from dbo.March2010 A
where A.Date >= Convert(datetime, '2010-04-01' )
在您的查询中,2010-4-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减4减1是2005将其转换为适当的 datetime,并使用单引号将解决此问题.)
(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'
它会为您进行转换,但在我看来,它不如为您之后的维护程序员显式转换为 DateTime 更具可读性.
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 中查询大于某个日期的所有日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 SQL Server 中查询大于某个日期的所有日期?
基础教程推荐
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
