2014-11-17 56 views
-1

实施例一个查询选择这个日期范围:February 1 2013 - February 28, 2013 AND February 1, 2014 - February 28, 2014SQL查询:选择破碎日期范围

示例代码

select 
    tenantcode, 
    date 
from DAILY 
where 
    tenantcode = 'CMBINA15' 
    AND date between '02/01/2013' and '02/28/2013' 
    and '02/01/2014' and '02/28/2014' 

的代码不返回当然任何值。

+0

表示您想要给定日期范围之间的日期列表吗? – Ajay2707

+0

你只需要在两者之间指定日期范围。你可以使用谷歌。 –

+0

经过数小时的搜索后,我无法找到答案,因为查询非常罕见。请阅读 – rickyProgrammer

回答

1

使用格式为'yyyy-MM-dd'CAST([Mydate] as Date)

select tenantcode, date from DAILY where tenantcode = 'CMBINA15' 
AND CAST([date] as Date) between 
(
CAST('2013-02-01' as DATE) and CAST('2013-28-02' as DATE)) 
OR CAST([date] as Date) between 
CAST('2014-02-01' as DATE) and CAST('2014-28-02' as DATE)) 
+0

也感谢回答 – rickyProgrammer

2

试试这个。

SELECT tenantcode, 
     date 
FROM DAILY 
WHERE tenantcode = 'CMBINA15' 
     AND (date BETWEEN '02/01/2013' AND '02/28/2013' 
       OR date BETWEEN '02/01/2014' AND '02/28/2014') 
+0

感谢这个工作!在我原来的代码中,我使用AND和通过其规则两个条件应该是真实的,我忘了使用OR,并且它做得很好,期望的输出得到满足,谢谢 – rickyProgrammer

+0

@rickyProgrammer快乐它帮助 –

0

你必须使用转换或转换函数来获取两侧的适当日期格式。所以它会匹配并给出结果。

或者您可以使用传递非日期值时使用的ISDATE函数。

select ISDATE('02/01/2013'), CONVERT(varchar(10), @frdate1 , 101) 

select tenantcode, date from DAILY where tenantcode = 'CMBINA15' 
AND 
( 
    CONVERT(varchar(10),[date] ,101) between 
     CONVERT(varchar(10), '2013-02-01' ,101) and CONVERT(varchar(10), '2013-28-02' ,101) 
    OR 
    CONVERT(varchar(10),[date] ,101) between 
     CONVERT(varchar(10), '2014-02-01' ,101) and CONVERT(varchar(10), '2014-28-02' ,101) 
) 
+0

嗨,谢谢对于这一点,关于格式化,没有任何问题,只想实现就是为查询给我一个破碎日期范围的结果。谢谢 – rickyProgrammer