如何在sql
中的两个日期之间计算(第5天和第20天)例如,如果从日期是05/05/2013到日期是2013年7月20日
结果是:5
答案 0 :(得分:1)
Declare @dtfrom as Datetime, @dtTo as Datetime
select @dtFrom = cast('5/5/2013' as datetime), @dtTo = cast('07/20/2013' as datetime)
;with dates(selectedDate,selectedday) as(
select @dtfrom,Day(@dtfrom)
union All
select DateAdd(day,1,selectedDate), day(DateAdd(day,1,selectedDate)) from dates where selectedDate < @dtTo
)
select Count(*) from dates where selectedday in(5,20)
答案 1 :(得分:0)
试试:
SELECT COUNT(*)
FROM your_table
WHERE DATEPART('d', date_column) IN (5, 20)
AND date_column BETWEEN '01/01/2013' and '30/12/2013'
01/01/2013
和30/12/2013
是您的范围。