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'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值