1. 程式人生 > >sql server 時間表的生成

sql server 時間表的生成

1、時間程式碼生成

ALTER PROCEDURE [dbo].[SP_CREATE_TIME_DIMENSION]
  @begin_date   AS DATE ,
  @end_date     AS DATE 
AS
declare 
@dDate date=convert(date,@begin_date),
@v_the_date varchar(10),
@v_the_year varchar(4),
@v_the_quarter varchar(2),
@v_the_month varchar(10),
@v_the_month2 varchar(2),
@v_the_week varchar(2),
@v_the_day varchar(10),
@v_the_day2 varchar(2),
@v_week_day nvarchar(10),
@adddays int=1;
WHILE (@dDate<=convert(date,@end_date))
begin
set @v_the_date=convert(char(10),@dDate,112);--key值
set @v_the_year=DATEPART("YYYY",@dDate);--年
set @v_the_quarter=DATEPART("QQ",@dDate);--季度"
set @v_the_month=DATEPART("MM",@dDate);--月份(字元型)
--set @v_the_month2=to_number(to_char(dDate, 'mm'));--月份(數字型)
set @v_the_day=DATEPART("dd",@dDate);--日(字元型)
--set @v_the_day2=to_char(dDate, 'dd');
set @v_the_week=DATEPART("WW",@dDate);--年的第幾周
set @v_week_day=DATEPART("DW",@dDate); --星期幾
insert into Dim_time(the_date,date_name,the_year,year_name,the_quarter,quarter_name,the_month,month_name,the_week,week_name,week_day,week_day_name,the_day)
values(
@v_the_date,
CASE when @v_the_month>=10 AND @v_the_day>=10
	THEN CONVERT(nvarchar(4),@v_the_year)+'-'+CONVERT(nvarchar(2),@v_the_month)+'-'+CONVERT(nvarchar(4),@v_the_day)+' 00:00:00.0000000'
	when @v_the_month>=10 AND @v_the_day<10 
	THEN CONVERT(nvarchar(4),@v_the_year)+'-'+CONVERT(nvarchar(2),@v_the_month)+'-0'+CONVERT(nvarchar(4),@v_the_day)+' 00:00:00.0000000'
	when @v_the_month<10 AND @v_the_day>=10 
	THEN CONVERT(nvarchar(4),@v_the_year)+'-0'+CONVERT(nvarchar(2),@v_the_month)+'-'+CONVERT(nvarchar(4),@v_the_day)+' 00:00:00.0000000'
	when @v_the_month<10 AND @v_the_day<10 
	THEN CONVERT(nvarchar(4),@v_the_year)+'-0'+CONVERT(nvarchar(2),@v_the_month)+'-0'+CONVERT(nvarchar(4),@v_the_day)+' 00:00:00.0000000'
END,
@v_the_year,
convert(nvarchar(10),@v_the_year)+'年',
@v_the_quarter,
'第'+convert(nvarchar(10),@v_the_quarter)+'季度',
case  when @v_the_month>=10 then 
convert(int,(convert(nvarchar(10),@v_the_year)+convert(nvarchar(10),@v_the_month)))
else convert(int,'0'+convert(nvarchar(10),@v_the_month)) end,
convert(nvarchar(10),@v_the_month)+'月',
@v_the_week
,'第'+convert(nvarchar(10),@v_the_week)+'周',
@v_week_day,
case @v_week_day-1 
when 1 then '星期一' 
when 2 then '星期二' 
when 3 then '星期三'
when 4 then '星期四' 
when 5 then '星期五' 
when 6 then '星期六'
when 0 then '星期日'
else '' end,
@v_the_day
);
set @dDate=dateadd(day,@adddays,@dDate);
continue
if @dDate=dateadd(day,-1,convert(date,@end_date))
break
  -- routine body goes here, e.g.
  -- SELECT 'Navicat for SQL Server'
END

節假日設定

---2017-元旦
--SELECT * from The_Date WHERE the_Date >='2017-01-01' AND the_Date<='2017-01-02'
--UPDATE The_Date SET holiday='元旦' WHERE the_Date >='2017-01-01' AND the_Date<='2017-01-02'
---2017-春節
--SELECT * from The_Date WHERE the_Date >='2017-01-27' AND the_Date<='2017-02-02'
--UPDATE The_Date SET holiday='春節' WHERE the_Date >='2017-01-27' AND the_Date<='2017-02-02'
---2017--清明節
--SELECT * from The_Date WHERE the_Date >='2017-04-02' AND the_Date<='2017-04-04'
--UPDATE The_Date SET holiday='清明節' WHERE the_Date >='2017-04-02' AND the_Date<='2017-04-04'
---2017--勞動節
--SELECT * from The_Date WHERE the_Date >='2017-05-01' AND the_Date<='2017-05-03'
--UPDATE The_Date SET holiday='勞動節' WHERE the_Date >='2017-05-01' AND the_Date<='2017-05-03'
---2017--端午節
--SELECT * from The_Date WHERE the_Date >='2017-05-28' AND the_Date<='2017-05-30'
--UPDATE The_Date SET holiday='端午節' WHERE the_Date >='2017-05-28' AND the_Date<='2017-05-30'
---2017--國慶節、中秋節
--SELECT * from The_Date WHERE the_Date >='2017-10-01' AND the_Date<='2017-10-08'
--UPDATE The_Date SET holiday='國慶節' WHERE the_Date >='2017-10-01' AND the_Date<='2017-10-08'
--UPDATE The_Date SET holiday='中秋節' WHERE the_Date >='2017-10-04' AND the_Date<='2017-10-04'

---2018-元旦
--SELECT * from The_Date WHERE the_Date >='2017-12-30' AND the_Date<='2018-01-01'
--UPDATE The_Date SET holiday='元旦' WHERE the_Date >='2017-12-30' AND the_Date<='2018-01-01'
---2018-春節
--SELECT * from The_Date WHERE the_Date >='2018-02-15' AND the_Date<='2018-02-21'
--UPDATE The_Date SET holiday='春節' WHERE the_Date >='2018-02-15' AND the_Date<='2018-02-21'
---2018--清明節
--SELECT * from The_Date WHERE the_Date >='2018-04-05' AND the_Date<='2018-04-07'
--UPDATE The_Date SET holiday='清明節' WHERE the_Date >='2018-04-05' AND the_Date<='2018-04-07'
---2018--勞動節
--SELECT * from The_Date WHERE the_Date >='2018-04-29' AND the_Date<='2018-05-01'
--UPDATE The_Date SET holiday='勞動節' WHERE the_Date >='2018-04-29' AND the_Date<='2018-05-01'
---2018--端午節
--SELECT * from The_Date WHERE the_Date >='2018-06-16' AND the_Date<='2018-06-18'
--UPDATE The_Date SET holiday='端午節' WHERE the_Date >='2018-06-16' AND the_Date<='2018-06-18'
---2018--中秋節
--SELECT * from The_Date WHERE the_Date >='2018-09-22' AND the_Date<='2018-09-24'
--UPDATE The_Date SET holiday='中秋節' WHERE the_Date >='2018-09-22' AND the_Date<='2018-09-24'
---2018--國慶節
--SELECT * from The_Date WHERE the_Date >='2018-10-01' AND the_Date<='2018-10-07'
--UPDATE The_Date SET holiday='國慶節' WHERE the_Date >='2018-10-01' AND the_Date<='2018-10-07'

---2019-元旦
--SELECT * from The_Date WHERE the_Date >='2018-12-30' AND the_Date<='2019-01-01'
--UPDATE The_Date SET holiday='元旦' WHERE the_Date >='2018-12-30' AND the_Date<='2019-01-01'
---2019-春節
--SELECT * from The_Date WHERE the_Date >='2019-02-04' AND the_Date<='2019-02-10'
--UPDATE The_Date SET holiday='春節' WHERE the_Date >='2019-02-04' AND the_Date<='2019-02-10'
---2019--清明節
--SELECT * from The_Date WHERE the_Date >='2019-04-05' AND the_Date<='2019-04-07'
--UPDATE The_Date SET holiday='清明節' WHERE the_Date >='2019-04-05' AND the_Date<='2019-04-07'
---2019--勞動節
--SELECT * from The_Date WHERE the_Date >='2019-04-29' AND the_Date<='2019-05-01'
--UPDATE The_Date SET holiday='勞動節' WHERE the_Date >='2019-04-29' AND the_Date<='2019-05-01'
---2019--端午節
--SELECT * from The_Date WHERE the_Date >='2019-06-07' AND the_Date<='2019-06-09'
--UPDATE The_Date SET holiday='端午節' WHERE the_Date >='2019-06-07' AND the_Date<='2019-06-09'
---2019--中秋節
--SELECT * from The_Date WHERE the_Date >='2019-09-13' AND the_Date<='2019-09-15'
--UPDATE The_Date SET holiday='中秋節' WHERE the_Date >='2019-09-13' AND the_Date<='2019-09-15'
---2019--國慶節
--SELECT * from The_Date WHERE the_Date >='2019-10-01' AND the_Date<='2019-10-07'
--UPDATE The_Date SET holiday='國慶節' WHERE the_Date >='2019-10-01' AND the_Date<='2019-10-07'