我想更新名为financialweekday
的表,其中包含以下列:
ID
Year
StartDate
EndDate
WeekNo
ID
是一个自动生成的数字,weekno
表示特定日期属于哪一周。我想更新weekno
日期为2014年12月29日至2015年12月27日的整年。
示例数据
ID Year StartDate EndDate WeekNo
1060 2015 28/12/2014 00:00 28/12/2014 23:59 1
1061 2015 29/12/2014 00:00 29/12/2014 23:59 1
1062 2015 30/12/2014 00:00 30/12/2014 23:59 1
1063 2015 31/12/2014 00:00 31/12/2014 23:59 1
1064 2015 01/01/2015 00:00 01/01/2015 23:59 1
1065 2015 02/01/2015 00:00 02/01/2015 23:59 1
1066 2015 03/01/2015 00:00 03/01/2015 23:59 1
1067 2015 04/01/2015 00:00 04/01/2015 23:59 2
1068 2015 05/01/2015 00:00 05/01/2015 23:59 2
任何建议都会有所帮助。
答案 0 :(得分:2)
似乎你想要:
SELECT ID,
[Year],
StartDate,
EndDate,
DATEDIFF(DAY,'20141229',StartDate)/7+1 WeekNo
FROM dbo.YourTable
答案 1 :(得分:0)
我想你在这里试图解决的问题是,本周的前几天是在上一个日历年。
在您的示例数据中,2015年的第一周从星期四开始,因此您希望将整周从星期一/星期日算作第1周。
由于您似乎拥有包含年份的列和计算周数的相关日期,因此您只需使用{{Year
列与StartDate
列的年份进行比较即可3}}。如果它们不同,则输出1,否则您将获取SQL Server为您提供的周数。
示例:
SELECT DATEPART(WEEK, '20150101')
-- Outputs: 1
SELECT DATEPART(WEEK, '20141229')
-- Ouptuts: 53
所以你想要第二个例子给你:1
使用您的示例数据,您可以运行它以产生所需的结果:
CREATE TABLE #financialweekday
(
[ID] INT ,
[Year] INT ,
[StartDate] DATETIME ,
[EndDate] DATETIME
);
INSERT INTO #financialweekday
( [ID], [Year], [StartDate], [EndDate])
VALUES ( 1060, 2015, '20141228 00:00', '20141228 23:59'),
( 1061, 2015, '20141229 00:00', '20141229 23:59'),
( 1062, 2015, '20141230 00:00', '20141230 23:59'),
( 1063, 2015, '20141231 00:00', '20141231 23:59'),
( 1064, 2015, '20150101 00:00', '20150101 23:59'),
( 1065, 2015, '20150102 00:00', '20150102 23:59'),
( 1066, 2015, '20150103 00:00', '20150103 23:59'),
( 1067, 2015, '20150104 00:00', '20150104 23:59'),
( 1068, 2015, '20150105 00:00', '20150105 23:59');
SELECT * ,
CASE WHEN DATEPART(YEAR, StartDate) = [Year] - 1 THEN 1
ELSE DATEPART(WEEK, StartDate)
END AS WeekNo
FROM #financialweekday
DROP TABLE #financialweekday
<强>产地:强>
ID Year StartDate EndDate WeekNo
1060 2015 2014-12-28 00:00:00.000 2014-12-28 23:59:00.000 1
1061 2015 2014-12-29 00:00:00.000 2014-12-29 23:59:00.000 1
1062 2015 2014-12-30 00:00:00.000 2014-12-30 23:59:00.000 1
1063 2015 2014-12-31 00:00:00.000 2014-12-31 23:59:00.000 1
1064 2015 2015-01-01 00:00:00.000 2015-01-01 23:59:00.000 1
1065 2015 2015-01-02 00:00:00.000 2015-01-02 23:59:00.000 1
1066 2015 2015-01-03 00:00:00.000 2015-01-03 23:59:00.000 1
1067 2015 2015-01-04 00:00:00.000 2015-01-04 23:59:00.000 2
1068 2015 2015-01-05 00:00:00.000 2015-01-05 23:59:00.000 2
注意:我已使用正确的日期格式格式化日期:YYYYMMDD HH:MM
,因此如果它们未作为日期时间存储,则可能需要应用一些格式(应该是)。
然后,您可以在更新语句中使用相同的逻辑:
CREATE TABLE #financialweekday
(
[ID] INT ,
[Year] INT ,
[StartDate] DATETIME ,
[EndDate] DATETIME ,
[WeekNo] INT
);
INSERT INTO #financialweekday
( [ID], [Year], [StartDate], [EndDate], WeekNo)
VALUES ( 1060, 2015, '20141228 00:00', '20141228 23:59', 0 ),
( 1061, 2015, '20141229 00:00', '20141229 23:59', 0 ),
( 1062, 2015, '20141230 00:00', '20141230 23:59', 0 ),
( 1063, 2015, '20141231 00:00', '20141231 23:59', 0 ),
( 1064, 2015, '20150101 00:00', '20150101 23:59', 0 ),
( 1065, 2015, '20150102 00:00', '20150102 23:59', 0 ),
( 1066, 2015, '20150103 00:00', '20150103 23:59', 0 ),
( 1067, 2015, '20150104 00:00', '20150104 23:59', 0 ),
( 1068, 2015, '20150105 00:00', '20150105 23:59', 0 );
UPDATE #financialweekday
SET WeekNo = CASE WHEN DATEPART(YEAR, StartDate) = [Year] - 1 THEN 1
ELSE DATEPART(WEEK, StartDate)
END
SELECT *
FROM #financialweekday
DROP TABLE #financialweekday