从特定时期开始的周计算

时间:2012-08-02 10:48:44

标签: sql sql-server

假设会计年度从7月/ 1月开始到6月30日。

我必须计算一周没有。因此在SQL Server 2005中。

请建议!

非常感谢!

5 个答案:

答案 0 :(得分:1)

如果您的财政年度第1周始终是7月1日至7月7日...

DECLARE
  @inputDate    DATETIME,
  @fYearStart   DATETIME,
  @weekNumber   INT
SET
  @inputDate    = getDate()
SET
  @fYearStart   = DATEADD(year, DATEDIFF(year, '20000101', DATEADD(month, -7, @inputDate)), '20000701')
SET
  @weekNumber   = DATEDIFF(day, @fYearStart, @inputDate) / 7 + 1


如果你的财务周总是周日到周六......

DECLARE
  @inputDate    DATETIME,
  @fYearStart   DATETIME,
  @weekNumber   INT
SET
  @inputDate    = getDate()
SET
  @fYearStart   = DATEADD(year, DATEDIFF(year, '20000101', DATEADD(month, -7, @inputDate)), '20000701')
SET
  @weekNumber   = DATEDIFF(WEEK, @fYearStart, @inputDate)


其中一个或另一个应适用于 week number的定义。

答案 1 :(得分:1)

试试这个,并根据自己的需要使用变量@dt:

DECLARE @dt DATETIME = GETDATE()
SELECT WeekOfMonth = DATEPART(wk, @dt) - DATEPART(wk,DATEADD(m, DATEDIFF(M, 0, @dt), 0)) + 1

EDITED: 我的错误,因为我错误地理解了这个问题,我的解决方案返回了当月的一周,而不是年份。

使用部分@Dems答案并在此处更改我的是一个完整的工作测试,根据日期和临时表上可用的会计年度开始输出3列WeekOfMonth,WeekOfYear和WeekOfFIscalYear。但我想一个财政年度的开始对于一个特定的公司来说总是一样的。我刚刚添加了不同的日期和年份进行测试。

DECLARE @TT TABLE (auxVal       INT,
                   auxdate      DATETIME,
                   fiscal_year  DATETIME
                )

INSERT @TT 
SELECT 100,'19120101 00:00:00','19120701' UNION ALL
SELECT 200,'18120615 00:00:00','18110701' UNION ALL
SELECT 100,'20121121 00:00:00','20120701' UNION ALL
SELECT 200,'20120101 00:00:00','20110701' UNION ALL
SELECT 100,'20150802 00:00:00','20140701' UNION ALL
SELECT 200,'20120330 00:00:00','20110701' UNION ALL
SELECT 322,'20110228 00:00:00','20100701'

SELECT DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, auxDate), 0), auxDate) + 1 WeekOfMonth,
       DATEPART( wk, auxDate) WeekOfYear,
       DATEDIFF(DAY, (DATEADD(YEAR, DATEDIFF(YEAR, fiscal_year, DATEADD(MONTH, -7, auxDate)), fiscal_year)), auxDate) / 7 + 1  WeekOfFiscalYear
  FROM @TT

结果:

WeekOfMonth   WeekOfYear   WeekOfFiscalYear
-------------------------------------------
      1            1             27
      3            25            51
      4            47            21
      1            1             27
      2            32            5
      5            13            40
      5            10            35
-------------------------------------------

答案 2 :(得分:0)

这个对我有用:

SELECT dt AS DayDate,
CONVERT(VARCHAR,DATENAME(weekday,dt)) AS [DayOfWk],
CONVERT(int,CONVERT(VARCHAR,DATEPART(yyyy,dt)) + RIGHT('00' + CONVERT(VARCHAR,DATEPART(wk,dt)),2)) AS [WeekID],
'Week ' + CONVERT(VARCHAR,DATEPART(wk,dt))AS [WeekDsc],
DATEPART(wk,dt) AS [WeekNbr],
DATEADD(dd, -(DATEPART(dw, dt)-1), dt) AS [WeekStartDay],
DATEADD(dd, 7-(DATEPART(dw, dt)), dt) AS [WeekEndDay],
CONVERT(VARCHAR,CASE WHEN DATEPART(mm,dt) > 6 THEN DATEPART(yyyy,dt) + 1 ELSE DATEPART(yyyy,dt) END) + RIGHT('00'+ CONVERT(VARCHAR,DATEDIFF(week,DATEADD(dd, 7-(DATEPART(dw, CASE WHEN DATEPART(mm,dt) > 6 THEN CONVERT(VARCHAR,DATEPART(yyyy,dt)) + '-07-01' ELSE CONVERT(VARCHAR,DATEPART(yyyy,dt) - 1) + '-07-01' END)), CASE WHEN DATEPART(mm,dt) > 6 THEN CONVERT(VARCHAR,DATEPART(yyyy,dt)) + '-07-01' ELSE CONVERT(VARCHAR,DATEPART(yyyy,dt) - 1) + '-07-01' END) + 1,DATEADD(dd, -(DATEPART(dw, dt)-1), dt)) + 2) ,2) AS [FinWkID]
FROM 
(
    SELECT DATEADD(day,increment,'2014-01-01') dt
    FROM
    (
        SELECT top 5000 ROW_NUMBER() OVER (ORDER BY s1.[object_id]) - 1 AS increment
        FROM sys.all_objects AS s1
        CROSS JOIN sys.all_objects AS s2
        ORDER BY s1.[object_id]
    ) SQ
) DATE_GENERATOR

答案 3 :(得分:0)

无需创建表格。 只需将@date替换为列日期名称

即可
declare @date datetime
set     @date = '12/8/2016 00:00:00'

select  case when datepart(month,@date) > 6 then 

            case when CEILING(datediff(day,'6/30/' + cast(datepart(year,@date) as nvarchar(4)),@date) / 7.0) > 52
            then 52 else CEILING(datediff(day,'6/30/' + cast(datepart(year,@date) as nvarchar(4)),@date) / 7.0) end

        else
            case when CEILING(datediff(day,'6/30/' + cast(datepart(year,@date)-1 as nvarchar(4)),@date) / 7.0) > 52
            then 52 else CEILING(datediff(day,'6/30/' + cast(datepart(year,@date)-1 as nvarchar(4)),@date) / 7.0) end
        end WorkWeek

Compute Week number based on Fiscal Year

答案 4 :(得分:-1)

try this

    select DATEPART(week,getdate())-DATEPART(week,'07-01-2012')

To get the week number starting from July 1st 2012