我正在使用SQL Server 2008 R2。我需要得到Sunday weekno并且我已经有了查询,但它显示了错误的数据。我该怎么办?
它显示2而不是1.例如,2013年1月6日(WeekNo - 1)
我的查询是
SELECT DATEPART (week, '01-13-2013 23:59:59.000' ) As WeekNo1 ,
DATEPART (week, '01-06-2013 23:59:59.000' ) As WeekNo2 ,
DATEPART (week, '12-30-2012 23:59:59.000' ) As WeekNo3 ,
DATEPART (week, '12-23-2012 23:59:59.000' ) As WeekNo4 ,
DATEPART (week, '12-16-2012 23:59:59.000' ) As WeekNo5 ,
DATEPART (week, '12-09-2012 23:59:59.000' ) As WeekNo6
答案 0 :(得分:2)
您需要将DATEFIRST
属性设置为一周的开始日期。这指定了一周的第一天。
要确定服务器一周的第一天是什么,您将使用:
SELECT @@DATEFIRST
然后,如果您需要更改该值,请使用:
SET DATEFIRST 1;
SELECT DATEPART (week, '01-13-2013 23:59:59.000' ) As WeekNo1 ,
DATEPART (week, '01-06-2013 23:59:59.000' ) As WeekNo2 ,
DATEPART (week, '12-30-2012 23:59:59.000' ) As WeekNo3 ,
DATEPART (week, '12-23-2012 23:59:59.000' ) As WeekNo4 ,
DATEPART (week, '12-16-2012 23:59:59.000' ) As WeekNo5 ,
DATEPART (week, '12-09-2012 23:59:59.000' ) As WeekNo6
答案 1 :(得分:1)
查询:
<强> SQLFIDDLEExample 强>
SET DATEFIRST 1
SELECT DATEPART (week, '01-13-2013 23:59:59.000' ) As WeekNo1 ,
DATEPART (week, '01-06-2013 23:59:59.000' ) As WeekNo2 ,
DATEPART (week, '12-30-2012 23:59:59.000' ) As WeekNo3 ,
DATEPART (week, '12-23-2012 23:59:59.000' ) As WeekNo4 ,
DATEPART (week, '12-16-2012 23:59:59.000' ) As WeekNo5 ,
DATEPART (week, '12-09-2012 23:59:59.000' ) As WeekNo6
结果:
| WEEKNO1 | WEEKNO2 | WEEKNO3 | WEEKNO4 | WEEKNO5 | WEEKNO6 |
-------------------------------------------------------------
| 2 | 1 | 53 | 52 | 51 | 50 |