如何获取两个范围之间的数据日期

时间:2022-07-16 21:29:13

In SQL I write a SELECT statement to fetch data between two range dates, using between and ..if i use betwenn ('01/01/2013') and ('31/12/2015') i get all data but i want just between the specified dates...exclude year of 2014

在SQL中我写一个SELECT语句来获取两个范围日期之间的数据,使用之间和..如果我使用betwenn('01 / 01/2013')和('31 / 12/2015')我得到所有数据,但我想要仅在指定日期之间...排除2014年

Ex:

例如:

select * 
from xxx 
 where  date between ('01/01/2013') and ('31/12/2013')
                        and  date between ('01/01/2015') and ('31/12/2015')

But it returned 0 rows.

但它返回0行。

2 个解决方案

#1


3  

Use or instead of and

使用或代替和

select * 
from your_table
where date between ('01/01/2013') and ('31/12/2013')
   or date between ('01/01/2015') and ('31/12/2015')

You don't want data that is in both date ranges what is not possible at the same time. You want data that is in either one of them.

您不希望两个日期范围内的数据同时无法实现。您需要其中任何一个中的数据。

#2


1  

A succinct way that may not perform as well is:

一种可能表现不佳的简洁方法是:

select * 
from xxx 
where Year(date) in (2013, 2015)

#1


3  

Use or instead of and

使用或代替和

select * 
from your_table
where date between ('01/01/2013') and ('31/12/2013')
   or date between ('01/01/2015') and ('31/12/2015')

You don't want data that is in both date ranges what is not possible at the same time. You want data that is in either one of them.

您不希望两个日期范围内的数据同时无法实现。您需要其中任何一个中的数据。

#2


1  

A succinct way that may not perform as well is:

一种可能表现不佳的简洁方法是:

select * 
from xxx 
where Year(date) in (2013, 2015)