简体   繁体   English

SQL查询返回两个日期之间的行

[英]SQL query to return the rows between two dates

I am using this query to get the rows between two dates but I don't get the rows from the database, it returns an empty set (in db I have rows for that day) 我正在使用此查询来获取两个日期之间的行但我没有从数据库中获取行,它返回一个空集(在db中我有那天的行)

Query: 查询:

select email 
from users
where date_email_sent between '17-May-12' AND '17-May-12'

When I try the query below I am getting 17th row alone 当我尝试下面的查询时,我将独自获得第17行

Query: 查询:

select email
from users
where date_email_sent between '17-May-12' AND '18-May-12'

Can any one plz suggest me how to get 17th records alone if start date and end date as same. 如果开始日期和结束日期相同,任何人都可以建议我如何单独获得第17条记录。

Thanks in advance. 提前致谢。

Does the date_email_sent have a datetime value? date_email_sent是否具有日期时间值? If the column value like 2012-05-17 09:30:00.000 has both date and time, then you may need to put the time together with your date value in the where clause, 如果像2012-05-17 09:30:00.000这样的列值同时包含日期和时间,那么您可能需要将时间与日期值一起放在where子句中,

eg 例如

where date_email_sent between '17-May-12 00:00:00.000' AND '17-May-12 23:59:59.000'

or you can look at the date value only for the field 或者您只能查看该字段的日期值

where DATEADD(dd, 0, DATEDIFF(dd, 0, date_email_sent )) between '17-May-12' AND '17-May-12'

SQL always consider as 12am if you put date only, so there will be a problem if you want to compare a date with datetime value 如果只放置日期,SQL总是考虑为12am,因此如果要将日期与datetime值进行比较,则会出现问题

When you specify a date without a time portion, the time is automatically assumed as 12:00 am. 指定没有时间部分的日期时,时间自动假定为12:00 am。 So when you write 所以当你写作

date_email_sent between '17-May-12' AND '17-May-12'

This is effectively the same as 这实际上和

date_email_sent between '17-May-12 12:00AM' AND '17-May-12 12:00AM'

So as you can see, the two times are identical and naturally there are no records in the specified interval. 正如您所看到的,这两次是相同的,并且在指定的时间间隔内自然没有记录。

If you want all the records on one day, you need to measure from midnight until midnight the next day: 如果您想要在一天内完成所有记录,则需要从第二天的午夜到午夜进行测量:

date_email_sent >= '17-May-12 12:00AM' and date_email_sent < '18-May-12 12:00AM'

or just: 要不就:

date_email_sent >= '17-May-12' and date_email_sent < '18-May-12'

Alternatively, you can extract the day portion of the date and check that for the correct value. 或者,您可以提取日期的日期部分并检查该值是否正确。 The specific date handling functions vary depending on your dbms. 具体的日期处理功能取决于您的dbms。

In SQL Server you should do it like this. 在SQL Server中,您应该这样做。

select email 
from users
where date_email_sent >= '20120517' and 
      date_email_sent < dateadd(day, 1, '20120517')

If you use something else you have to replace dateadd with something else. 如果你使用其他东西,你必须用其他东西替换dateadd

select email 
from users
where date_email_sent >= '17-May-12' AND date_email_sent < '18-May-12'

when you write BETWEEN '17-May-12' AND '17-May-12' so you haven't mentioned the time so in Date datatype it considered as 当你写下'17 -May-12'和'17-May-12'时,你没有提到时间,所以在Date数据类型中它被认为是

'17-May-12 00:00:00' AND '17-May-12 00:00:00' '17 -May-12 00:00:00'和'17-May-12 00:00:00'

so your range is limited that's why you didn't get records. 所以你的范围是有限的,这就是你没有得到记录的原因。 you should mention time also to get the record with same date . 你还应该提一下时间来获得同一日期的记录。 In oracle you can write like this 在oracle中你可以像这样写

BETWEEN to_date('dd-mm-yyyy hh24:mi','17-05-12 00:00') AND to_date('dd-mm-yyyy hh24:mi','17-05-12 23:59')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM