简体   繁体   English

如何在 SQL 服务器中查询大于特定日期的所有日期?

[英]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.

相关问题 如何使用 Microsoft SQL Server 2008 获取日期比今天的日期大 6 个月的所有记录? - How do I get all records where date is 6 months greater than today's date, using Microsoft SQL Server 2008? 如何创建大于一个日期而不是其他日期之间的查询? - How do I create a query with greater than one date and not between some other dates? 如何仅从SQL Server的XML列中检索日期大于今天的日期的行? - How can I only retrieve rows with dates greater than today's date from an XML column in SQL Server? SQL Server 创建日期大于一个日期的标志 - SQL Server creating a flag with dates greater than one date SQL Server-日期,大于和小于 - SQL Server - Dates, Greater than and Less than 查询多个日期,不大于当前日期 - Query multiple dates NOT greater than the current date SQL 查询两个日期之差大于某个值时删除记录 - SQL query to delete records when the difference between two dates is greater than a certain value 如何有效删除与另一条记录相关且大于某个阈值的所有记录? - How do I delete all records related another record and greater than a certain threshold efficiently? 如何在SQL中使用datetime选择大于特定时间的那些条目? - How I do select those entries in SQL which are greater than a certain time using datetime? SQL-大于日期查询不起作用 - SQL - Greater than date query not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM