標籤:
--建了一張表
if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[Bas_DateTime]‘) and OBJECTPROPERTY(id, N‘IsUserTable‘) = 1)
drop table [dbo].[Bas_DateTime]
GO
CREATE TABLE [dbo].[Bas_DateTime] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Rq] [datetime] NULL ,
[YearOfRq] [varchar] (20) NULL ,
[HalfYear] [varchar] (20) NULL ,
[QuarterOfYear] [varchar] (20) NULL ,
[MonthOfYear] [varchar] (20) NULL ,
[WeekOfHalfYear] [varchar] (20) NULL ,
[WeekOfYear] [varchar] (20) NULL ,
[WeekOfQuarter] [varchar] (20) NULL ,
[WeekOfMonth] [varchar] (20) NULL ,
[WeekDay] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
--向表中插入日期
declare @staDate datetime,
@endDate datetime
SET @stadate=‘2000-01-01‘ --起始日期
SET @endDate=‘2051-01-01‘ --結束日期
while @staDate < @endDate
begin
insert into Bas_DateTime(Rq) values (@stadate)
SET @[email protected]+1
end
Go
--YearofRq
update Bas_DateTime
set YearOfRq = ltrim(str(cast(Year(Rq)as int)))+‘年‘
go
--HalfYear
update Bas_DateTime
set HalfYear = case when month(Rq)>6 then ‘下半年‘ else ‘上半年‘ end
go
/*
--QuarterOfHalfYear
update Bas_DateTime
set QuarterOfHalfYear = case when (month(Rq) between 1 and 3 ) or (month(Rq) between 7 and 9 )then 1 else 2 end
go
*/
--QuarterOfYear
update Bas_DateTime
set QuarterOfYear = case
when (month(Rq) between 1 and 3 ) then ‘第一季度‘
when (month(Rq) between 4 and 6 ) then ‘第二季度‘
when (month(Rq) between 7 and 9 ) then ‘第三季度‘
else ‘第四季度‘ end
go
--WeekDay
update Bas_DateTime
set [WeekDay]= DateName(weekday,Rq)
go
--MonthOfHalfYear
/*
update Bas_DateTime
set MonthOfHalfYear = case when (Month(Rq) > 6 ) then (month(Rq)-6) else month(Rq) end
go*/
--MonthOfYear
update Bas_DateTime
set MonthOfYear = ltrim(str(cast(month(Rq)as int)))+‘月‘
go
--weekofyear
update Bas_DateTime
set WeekOfYear = ‘第‘+ltrim(str(cast(datepart(week,Rq)as int)))+‘周‘
go
/*declare @ datetime
set @ = ‘2007-08-09‘
select datename(weekday,dateadd(month,datediff(month,0,@),0)) as ‘周‘
*/
-- datediff(day,‘2007-08-01‘,‘2007-08-02‘)
-- dateadd(month,datediff(month,0,@),0) ‘ 所在月的第一天
--dateadd(quarter,datediff(quarter,0,@),0) as 所在季的第一天
-- dateadd(year,year(‘2007-02-9‘)-2000,‘2000-06-30‘) 以2000-06-30為基準,以後或得xxxx-06-30
--weekofmonth
update Bas_DateTime
set weekofmonth = case when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期一‘ then (case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<6 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<13 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=6 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<20 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=13 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<27 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=20 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<32 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=27 then ‘第5周‘
end )
when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期二‘ then( case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<5 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<12 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=5 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<19 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=12 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<26 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=19 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<32 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=26 then ‘第5周‘
end )
when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期三‘ then( case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<4 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<11 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=4 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<18 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=11 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<25 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=18 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<32 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=25 then ‘第5周‘
end )
when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期四‘ then(case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<3 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<10 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=3 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<17 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=10 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<24 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=17 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<31 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=24 then ‘第5周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq) >=31 then ‘第6周‘
end )
when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期五‘ then(case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<2 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<9 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=2 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<16 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=9 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<23 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=16 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<30 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=23 then ‘第5周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq) >=30 then ‘第6周‘
end )
when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期六‘ then(case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<1 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<8 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=1 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<15 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=8 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<22 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=15 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<29 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=22 then ‘第5周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq) >=29 then ‘第6周‘
end )
when datename(weekday,dateadd(month,datediff(month,0,rq),0))=‘星期日‘ then(case
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<7 then ‘第1周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<15 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=7 then ‘第2周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<22 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=15 then ‘第3周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<29 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=22 then ‘第4周‘
when datediff(day,dateadd(month,datediff(month,0,rq),0),rq)<32 and datediff(day,dateadd(month,datediff(month,0,rq),0),rq)>=29 then ‘第5周‘
end)
end
go
--weekofhalfyear
update Bas_DateTime
set weekofhalfyear = case when month(rq) < 7 then weekofyear
else case
when datename(weekday,dateadd(year,year(rq)-2000,‘2000-06-30‘))=‘星期六‘ then ‘第‘+ltrim(str(cast(datepart(week,Rq)-datepart(week,dateadd(year,year(rq)-2000,‘2000-06-30‘))as int)))+‘周‘
else ‘第‘+ltrim(str(cast(datepart(week,Rq)-datepart(week,dateadd(year,year(rq)-2000,‘2000-06-30‘)) + 1 as int)))+‘周‘ end
end
go
--weekofquarter
update Bas_DateTime
set weekofquarter= case
when month(Rq) between 1 and 3 then weekofyear
when month(Rq) between 4 and 6 then case
when datename(weekday,dateadd(year,year(rq)-2000,‘2000-03-31‘))=‘星期六‘ then ‘第‘+ltrim(str(cast(datepart(week,Rq)-datepart(week,dateadd(year,year(rq)-2000,‘2000-03-31‘))as int)))+‘周‘
else ‘第‘+ltrim(str(cast(datepart(week,Rq)-datepart(week,dateadd(year,year(rq)-2000,‘2000-03-31‘))+1 as int)))+‘周‘ end
when month(Rq) between 7 and 9 then weekofhalfyear
when month(Rq) between 10 and 12 then case
when datename(weekday,dateadd(year,year(rq)-2000,‘2000-09-30‘))=‘星期六‘ then ‘第‘+ltrim(str(cast(datepart(week,Rq)-datepart(week,dateadd(year,year(rq)-2000,‘2000-09-30‘))as int)))+‘周‘
else ‘第‘+ltrim(str(cast(datepart(week,Rq)-datepart(week,dateadd(year,year(rq)-2000,‘2000-09-30‘))+1 as int)))+‘周‘ end
end
go
select * from Bas_DateTime order by Rq
sql server 產生萬年曆