如何在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? 任何人都可以提供参考原因吗?


#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 这将避免与其他语言系统的任何问题,并将使用索引

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值