我有一个日历表,其中包含2000年到2012年的数据(2012年不是故意的!)。我只是意识到我没有月份的周数(例如2月1日1,2,3,4,2,3,4)
如何计算一个月内的周数来填写此表?
这是表格架构
CREATE TABLE [TCalendar] (
[TimeKey] [int] NOT NULL ,
[FullDateAlternateKey] [datetime] NOT NULL ,
[HolidayKey] [tinyint] NULL ,
[IsWeekDay] [tinyint] NULL ,
[DayNumberOfWeek] [tinyint] NULL ,
[EnglishDayNameOfWeek] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[SpanishDayNameOfWeek] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[FrenchDayNameOfWeek] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[DayNumberOfMonth] [tinyint] NULL ,
[DayNumberOfYear] [smallint] NULL ,
[WeekNumberOfYear] [tinyint] NULL ,
[EnglishMonthName] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[SpanishMonthName] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[FrenchMonthName] [nvarchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[MonthNumberOfYear] [tinyint] NULL ,
[CalendarQuarter] [tinyint] NULL ,
[CalendarYear] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[CalendarSemester] [tinyint] NULL ,
[FiscalQuarter] [tinyint] NULL ,
[FiscalYear] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[FiscalSemester] [tinyint] NULL ,
[IsLastDayInMonth] [tinyint] NULL ,
CONSTRAINT [PK_TCalendar] PRIMARY KEY CLUSTERED
(
[TimeKey]
) ON [PRIMARY]
) ON [PRIMARY]
GO
答案 0 :(得分:1)
update TCalendar
set = WeekNumberOfMonth = DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, FullDateAlternateKey), 0), FullDateAlternateKey) +1