sql中的周和日的日期差异

时间:2014-12-18 10:33:38

标签: sql sql-server-2008

我必须显示startdate和EndDate之间的持续时间 因为我使用 DATEDIFF 一周它会给我一个int值,如3周,2周 但是DATEDIFF的天数是23,那么结果就像 3周,2天

3 个答案:

答案 0 :(得分:1)

SELECT DATEDIFF(WEEK, @StartDate, @EndDate) as WeekCount,
    DATEDIFF(DAY, @StartDate, @EndDate) % 7 as DayCount

答案 1 :(得分:0)

尝试使用

SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days;

得到天数,然后除以7除以获得周数,%得到天数

答案 2 :(得分:0)

也许是这样的?

<强> Mysql的

SELECT concat(floor(DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36')/7)," Weeks ",mod(DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36'),7)," Days");

SQL Server

SELECT concat(floor(DATEDIFF(day, '2010-10-08 18:23:13', '2010-09-21 21:40:36')/7)," Weeks ",mod(DATEDIFF(day, '2010-10-08 18:23:13', '2010-09-21 21:40:36'),7)," Days");

结果为2 Weeks 3 Days

虽然我确信有更好的方法可以一次性完成差异,而不是DATEDIFF两次。

希望这有帮助

由于