关于Sybase数据库中时间表的创建和生成!

基本上用到了所有的日期函数,简单修改一下可以在SQLServer上运行了。但是也有很多的不足,欢迎提出宝贵意见!

--时间维度表或者时间基础表

CREATE TABLE DimTime (
TimeKey NUMERIC(18,0) IDENTITY ,
FullDateAlternateKey DATETIME NULL ,
DayNumberOfWeek TINYINT NULL ,
EnglishDayNameOfWeek VARCHAR(10) NULL ,
SpanishDayNameOfWeek VARCHAR(10) NULL ,
FrenchDayNameOfWeek VARCHAR(10) NULL ,
DayNumberOfMonth TINYINT NULL ,
DayNumberOfYear SMALLINT NULL ,
WeekNumberOfYear TINYINT NULL ,
EnglishMonthName VARCHAR(10) NULL ,
SpanishMonthName VARCHAR(10) NULL ,
FrenchMonthName VARCHAR(10) NULL ,
MonthNumberOfYear TINYINT NULL ,
CalENDarQuarter TINYINT NULL ,
CalENDarYear CHAR(4) NULL ,
CalENDarSemester TINYINT NULL ,
FiscalQuarter TINYINT NULL ,
FiscalYear CHAR(4) NULL ,
FiscalSemester TINYINT NULL
)
go

--时间存储过程
CREATE PROCEDURE p_create_DATETIME
@BeginStr VARCHAR(20) = NULL,
@EndStr VARCHAR(20) = NULL,
@FiscalStartStr VARCHAR(20) = NULL
AS
/*
@BeginStr define the BeginDate created,default will be maxdate FROM dimtime table,format is 'yyyy-mm-dd'
@EndStr define the EndDate created,default will the END date of this year,format is 'yyyy-mm-dd'
@FiscalStartStr define the BeginDate created,default will be '01-01',format is 'mm-dd'
关于财年的计算有点问题,是否1~6月份开始算成当年财年,而7~12月份的算成第二年的财年,也许还需要增加标志位
*/
DECLARE
@TmpBeginStr VARCHAR(20),
@TmpEndStr VARCHAR(20),
@TmpFiscalStartStr VARCHAR(20),
@BeginDate DATETIME,
@EndDate DATETIME,
@FiscalStartDate DATETIME,
@FiscalBaseDate DATETIME,
@DateDiffer INT

SELECT @TmpBeginStr = @BeginStr
SELECT @TmpEndStr = @EndStr
SELECT @TmpFiscalStartStr = @FiscalStartStr

IF @TmpBeginStr IS NULL
BEGIN
SELECT @BeginDate = DATEADD(dd,1,MAX(FullDateAlternateKey)) FROM DimTime
IF @BeginDate IS NULL
BEGIN
SELECT @TmpBeginStr = CONVERT(VARCHAR(20),getdate(),110)
SELECT @BeginDate = CONVERT(DATETIME,@TmpBeginStr)
END
END
else
BEGIN
SELECT @BeginDate = CONVERT(DATETIME,@TmpBeginStr)
END

IF @TmpEndStr IS NULL
BEGIN
SELECT @TmpEndStr = DATENAME(yy,getdate())+'-12-31'
END
SELECT @EndDate = CONVERT(DATETIME,@TmpEndStr)

IF @TmpFiscalStartStr IS NULL
BEGIN
SELECT @TmpFiscalStartStr = '01-01'
END
SELECT @TmpFiscalStartStr = DATENAME(yy,getdate())+ '-' + @TmpFiscalStartStr

SELECT @FiscalStartDate= CONVERT(DATETIME,@TmpFiscalStartStr)
SELECT @DateDiffer = DATEDIFF(dd,CONVERT(DATETIME,DATENAME(yy,getdate())+'-01-01'),@TmpFiscalStartStr)
SELECT @FiscalBaseDate = DATEADD(dd,-@DateDiffer,@BeginDate)

DELETE FROM DimTime WHERE FullDateAlternateKey >=@BeginDate and FullDateAlternateKey < @EndDate

WHILE @BeginDate < @EndDate
BEGIN
INSERT INTO DimTime
(
--TimeKey , --NUMERIC
FullDateAlternateKey, --DATETIME
DayNumberOfWeek, --TINYINT
EnglishDayNameOfWeek , --VARCHAR
SpanishDayNameOfWeek, --VARCHAR
FrenchDayNameOfWeek , --VARCHAR
DayNumberOfMonth , --TINYINT
DayNumberOfYear , --SMALLINT
WeekNumberOfYear , --TINYINT
EnglishMonthName , --VARCHAR
SpanishMonthName , --VARCHAR
FrenchMonthName , --VARCHAR
MonthNumberOfYear , --TINYINT
CalENDarQuarter , --TINYINT
CalENDarYear , --CHAR
CalENDarSemester , --TINYINT
FiscalQuarter , --TINYINT
FiscalYear , --CHAR
FiscalSemester --TINYINT
)
VALUES
(
--indetity TimeKey ,
@BeginDate,
DATEPART (dw , @BeginDate ) ,
DATENAME (dw , @BeginDate ) ,
'',
'',
DATEPART (dd , @BeginDate ) ,
DATEPART (dy , @BeginDate ) ,
DATEPART (wk , @BeginDate ) ,
DATENAME (mm , @BeginDate ) ,
'',
'',
DATEPART (mm, @BeginDate ) ,
DATEPART (qq , @BeginDate ) ,
CAST(DATEPART (yy , @BeginDate ) AS CHAR(4)),
(DATEPART (mm , @BeginDate )+5)/6 ,
DATEPART (qq , @FiscalBaseDate) ,
CAST(DATEPART (yy , @FiscalBaseDate) AS CHAR(4)),
(DATEPART (mm , @FiscalBaseDate)+5)/6
)
SELECT @BeginDate = dateadd(dd,1,@BeginDate)
SELECT @FiscalBaseDate = dateadd(dd,1,@FiscalBaseDate)
END


--执行方法
exec p_create_DATETIME NULL,NULL , '01-15'
exec p_create_DATETIME '2006-01-01','2006-10-02', '01-15'
SELECT * FROM DimTime

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6517/viewspace-145433/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6517/viewspace-145433/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值