连接DATEADD& DATEPART

时间:2016-02-03 14:49:07

标签: sql sql-server sql-server-2008 tsql

SQL Server 2008。 有没有办法连接下面的内容并删除00:00:00:000

*编辑 - 完整列包含日期列表

SELECT DISTINCT
    DATEADD(dd, -(DATEPART(dw, fulldate)-1), fulldate) [WeekStart], 
    DATEADD(dd, 7-    (DATEPART(dw, fulldate)), fulldate) [WeekEnd]
FROM time
WHERE YEAR(time) >= 2016
ORDER BY WeekStart

结果:

WeekOf

12-27-2015-01-02-2016

等等...

1 个答案:

答案 0 :(得分:1)

由于dateadd返回date而非字符串 - 您必须首先使用适当的format(在您的情况下为110)将值转换为字符串,然后将它们连接起来:

select distinct
    convert(nvarchar(20), DATEADD(dd, -(DATEPART(dw, fulldate)-1), fulldate), 110) + '-' + 
    convert(nvarchar(20), DATEADD(dd, 7-    (DATEPART(dw, fulldate)), fulldate), 110) as WeekOf
from time
where YEAR(time) >= 2016
order by WeekStart