我对SQL磕磕绊......看看你是否可以帮助5和6
业务需求
对数据的评论
样本数据
SQL Queseries
答案 0 :(得分:0)
这就是你需要的吗?
SELECT CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, DATEADD(year, -1, '05/06/2014')) - 6, DATEADD(year, -1, '05/06/2014'))), 101)
SELECT CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, DATEADD(year, -1, '05/06/2014')), DATEADD(year, -1, '05/06/2014'))), 101)
另外,如果是这种情况,我会使用一些变量。
declare @date Datetime = '05/06/2014'
declare @datePreviousYear Datetime = DATEADD(year, -1, @date)
declare @currentYeardateCalculated int = @@DATEFIRST - DATEPART(dw, @date)
declare @lastYearDateCalculated int = @@DATEFIRST - DATEPART(dw, @datePreviousYear)
--1
SELECT @date
--2
SELECT datepart(week,@date )
--3
SELECT CONVERT(varchar(50), (DATEADD(dd, @currentYeardateCalculated - 6, @date)), 101)
--4
SELECT CONVERT(varchar(50), (DATEADD(dd, @currentYeardateCalculated, @date)), 101)
--5
SELECT CONVERT(varchar(50), (DATEADD(dd, @lastYearDateCalculated - 6,@datePreviousYear)), 101)
--6
SELECT CONVERT(varchar(50), (DATEADD(dd, @lastYearDateCalculated , @datePreviousYear)), 101)