0

我正在使用 SQL Server 2008 R2。我需要获得周日 weekno 并且我已经有查询,但它显示了错误的数据。我能做些什么 ?

它显示 2 而不是 1。例如,06-Jan-2013(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 
4

2 回答 2

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 

请参阅SQL Fiddle with Demo

于 2013-01-24T12:05:37.573 回答
1

询问:

SQLFIDDLE示例

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 |
于 2013-01-24T12:04:49.447 回答