如何在两个日期之间获得Modays,Tuesdays等的数量

时间:2015-06-16 11:26:15

标签: mysql sql

嗨evryone我正在尝试使用算法how to calculate the number of Tuesdays between two dates in TSQL来计算两个日期之间的天数。

我只是用我的日期替换了一些参数,但是我收到了语法错误。我试图删除日期时间以使用日期但仍然出现错误

select 

datediff(day, -7, DATETIME('2015-06-30'))/7-datediff(day, -6,   DATETIME('2015-05-28'))/7 AS MON,

datediff(day, -6, DATETIME('2015-06-30'))/7-datediff(day, -5,DATETIME('2015-05-28'))/7 AS TUE,
datediff(day, -5,DATETIME('2015-06-30'))/7-datediff(day, -4, DATETIME('2015-05-28'))/7 AS WED,
datediff(day, -4, DATETIME('2015-06-30'))/7-datediff(day, -3, DATETIME('2015-05-28'))/7 AS THU,
datediff(day, -3,DATETIME('2015-06-30'))/7-datediff(day, -2,DATETIME('2015-05-28'))/7 AS FRI,
datediff(day, -2, DATETIME('2015-06-30'))/7-datediff(day, -1,DATETIME('2015-05-28'))/7 AS SAT,
datediff(day, -1,DATETIME('2015-06-30'))/7-datediff(day, 0, DATETIME('2015-05-28'))/7 AS SUN

1 个答案:

答案 0 :(得分:0)

您使用了错误的转换语法

我将您的Sql查询修改为下面的

select datediff(day, -7, CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, -6,  CONVERT(DATETIME,'2015-05-28'))/7 AS MON,
datediff(day, -6, CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, -5,CONVERT(DATETIME,'2015-05-28'))/7 AS TUE,
datediff(day, -5,CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, -4, CONVERT(DATETIME,'2015-05-28'))/7 AS WED,
datediff(day, -4, CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, -3,CONVERT(DATETIME,'2015-05-28'))/7 AS THU,
datediff(day, -3,CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, -2,CONVERT(DATETIME,'2015-05-28'))/7 AS FRI,
datediff(day, -2, CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, -1,CONVERT(DATETIME,'2015-05-28'))/7 AS SAT,
datediff(day, -1,CONVERT(DATETIME,'2015-06-30'))/7-datediff(day, 0, CONVERT(DATETIME,'2015-05-28'))/7 AS SUN

由于