sql server 產生萬年曆

來源:互聯網
上載者:User

標籤:

--建了一張表

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 產生萬年曆

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.