让数据库产生一张详细的日历表

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

让数据库产生一张详细的日历表

也许有了这张表,你的工作会轻松很多!

CREATE TABLE [dbo].[time_dimension] (
   [time_id] [int] IDENTITY (1, 1) NOT NULL ,
   [the_date] [datetime] NULL ,
   [the_day] [nvarchar] (15) NULL ,
   [the_month] [nvarchar] (15) NULL ,
   [the_year] [smallint] NULL ,
   [day_of_month] [smallint] NULL ,
   [week_of_year] [smallint] NULL ,
   [month_of_year] [smallint] NULL ,
   [quarter] [nvarchar] (2) NULL ,
   [fiscal_period] [nvarchar] (20) NULL
) ON [PRIMARY]

 
DECLARE @WeekString varchar(12),
@dDate SMALLDATETIME,
@sMonth varchar(20),
@iYear smallint,
@iDayOfMonth smallint,
@iWeekOfYear smallint,
@iMonthOfYear smallint,
@sQuarter varchar(2),
@sSQL varchar(100),
@adddays int
 
SELECT @adddays = 1 --日期增量(可以自由设定)
SELECT @dDate = '01/01/2002' --开始日期
 
WHILE @dDate < '12/31/2004'  --结束日期
BEGIN
 
   SELECT @WeekString = DATENAME (dw, @dDate)
   SELECT @sMonth=DATENAME(mm,@dDate)
   SELECT @iYear= DATENAME (yy, @dDate)
   SELECT @iDayOfMonth=DATENAME (dd, @dDate)
   SELECT @iWeekOfYear= DATENAME (week, @dDate)
   SELECT @iMonthOfYear=DATEPART(month, @dDate)
   SELECT @sQuarter = 'Q' +  CAST(DATENAME (quarter, @dDate)as varchar(1))

   INSERT INTO time_dimension(the_date, the_day, the_month, the_year,
   day_of_month,
   week_of_year, month_of_year, quarter) VALUES
   (@dDate, @WeekString, @sMonth, @iYear, @iDayOfMonth, @iWeekOfYear,
   @iMonthOfYear, @sQuarter)
   SELECT @dDate = @dDate + @adddays
END
GO

select * from time_dimension

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: 数据库
想对作者说点什么? 我来说一句

产生一张详细的日历表

zgqtxwd zgqtxwd

2008-04-28 04:05:00

阅读数:196

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭