if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[time_by_day]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[time_by_day]
GOCREATE TABLE [dbo].[time_by_day] (
[time_id] [int] IDENTITY (1, 1) NOT NULL ,
[the_date] [datetime] NULL ,
[the_day] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[the_month] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[the_year] [smallint] NULL ,
[day_of_month] [smallint] NULL ,
[week_of_year] [float] NULL ,
[month_of_year] [smallint] NULL ,
[quarter] [nvarchar] (2) COLLATE Chinese_PRC_CI_AS NULL ,
[tenday_of_month] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[half_year] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
) ON [PRIMARY]
GOtruncate table time_by_day
go
declare @DateJ int
declare @CurDate datetime
declare @StartDate datetime
declare @EndDate datetime
set @StartDate='2005-5-1' ---------这里填写起始时间
set @EndDate='2050-12-31' ---------这里填写结束时间
--SELECT DATEDIFF(day, @StartDate, @EndDate) into @DateJ
set @CurDate= @StartDate
while @CurDate<@EndDate BEGIN
insert into time_by_day (the_date,the_day,the_month,the_year,day_of_month,
week_of_year,month_of_year,quarter,tenday_of_month,half_year)
values (@CurDate,
case when DATEPART(weekday, @CurDate)=1 then '星期日'
when DATEPART(weekday, @CurDate)=2 then '星期一'
when DATEPART(weekday, @CurDate)=3 then '星期二'
when DATEPART(weekday, @CurDate)=4 then '星期三'
when DATEPART(weekday, @CurDate)=5 then '星期四'
when DATEPART(weekday, @CurDate)=6 then '星期五'
when DATEPART(weekday, @CurDate)=7 then '星期六'
end ,
case when DATEPART(Month, @CurDate)=1 then '一月'
when DATEPART(Month, @CurDate)=2 then '二月'
when DATEPART(Month, @CurDate)=3 then '三月'
when DATEPART(Month, @CurDate)=4 then '四月'
when DATEPART(Month, @CurDate)=5 then '五月'
when DATEPART(Month, @CurDate)=6 then '六月'
when DATEPART(Month, @CurDate)=7 then '七月'
when DATEPART(Month, @CurDate)=8 then '八月'
when DATEPART(Month, @CurDate)=9 then '九月'
when DATEPART(Month, @CurDate)=10 then '十月'
when DATEPART(Month, @CurDate)=11 then '十一月'
when DATEPART(Month, @CurDate)=12 then '十二月'
end ,
DATENAME(year, @CurDate) ,
DATENAME(day, @CurDate) ,
DATENAME(week, @CurDate),DATENAME(month, @CurDate) ,DATENAME(quarter, @CurDate)
,
case when DATEPART(day, @CurDate)<=10 then '上旬'
when DATEPART(day, @CurDate)<=20 and DATEPART(day, @CurDate)>10 then '中旬'
when DATEPART(day, @CurDate)>20 then '下旬'
end,
case when DATEPART(month, @CurDate)<=6 then '上半年'
when DATEPART(month, @CurDate)>6 then '下半年'
end
)
--print @CurDate
set @CurDate=DATEADD(day,1,@CurDate)
end
drop table [dbo].[time_by_day]
GOCREATE TABLE [dbo].[time_by_day] (
[time_id] [int] IDENTITY (1, 1) NOT NULL ,
[the_date] [datetime] NULL ,
[the_day] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[the_month] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[the_year] [smallint] NULL ,
[day_of_month] [smallint] NULL ,
[week_of_year] [float] NULL ,
[month_of_year] [smallint] NULL ,
[quarter] [nvarchar] (2) COLLATE Chinese_PRC_CI_AS NULL ,
[tenday_of_month] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[half_year] [nvarchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
) ON [PRIMARY]
GOtruncate table time_by_day
go
declare @DateJ int
declare @CurDate datetime
declare @StartDate datetime
declare @EndDate datetime
set @StartDate='2005-5-1' ---------这里填写起始时间
set @EndDate='2050-12-31' ---------这里填写结束时间
--SELECT DATEDIFF(day, @StartDate, @EndDate) into @DateJ
set @CurDate= @StartDate
while @CurDate<@EndDate BEGIN
insert into time_by_day (the_date,the_day,the_month,the_year,day_of_month,
week_of_year,month_of_year,quarter,tenday_of_month,half_year)
values (@CurDate,
case when DATEPART(weekday, @CurDate)=1 then '星期日'
when DATEPART(weekday, @CurDate)=2 then '星期一'
when DATEPART(weekday, @CurDate)=3 then '星期二'
when DATEPART(weekday, @CurDate)=4 then '星期三'
when DATEPART(weekday, @CurDate)=5 then '星期四'
when DATEPART(weekday, @CurDate)=6 then '星期五'
when DATEPART(weekday, @CurDate)=7 then '星期六'
end ,
case when DATEPART(Month, @CurDate)=1 then '一月'
when DATEPART(Month, @CurDate)=2 then '二月'
when DATEPART(Month, @CurDate)=3 then '三月'
when DATEPART(Month, @CurDate)=4 then '四月'
when DATEPART(Month, @CurDate)=5 then '五月'
when DATEPART(Month, @CurDate)=6 then '六月'
when DATEPART(Month, @CurDate)=7 then '七月'
when DATEPART(Month, @CurDate)=8 then '八月'
when DATEPART(Month, @CurDate)=9 then '九月'
when DATEPART(Month, @CurDate)=10 then '十月'
when DATEPART(Month, @CurDate)=11 then '十一月'
when DATEPART(Month, @CurDate)=12 then '十二月'
end ,
DATENAME(year, @CurDate) ,
DATENAME(day, @CurDate) ,
DATENAME(week, @CurDate),DATENAME(month, @CurDate) ,DATENAME(quarter, @CurDate)
,
case when DATEPART(day, @CurDate)<=10 then '上旬'
when DATEPART(day, @CurDate)<=20 and DATEPART(day, @CurDate)>10 then '中旬'
when DATEPART(day, @CurDate)>20 then '下旬'
end,
case when DATEPART(month, @CurDate)<=6 then '上半年'
when DATEPART(month, @CurDate)>6 then '下半年'
end
)
--print @CurDate
set @CurDate=DATEADD(day,1,@CurDate)
end
As
Begin
Declare @BeginDate DateTime,@EndDate DateTime
Set @BeginDate=@year+'0101'
Set @EndDate=@year+'1231'
Create Table #T (ListDay Varchar(10))
While @BeginDate<=@EndDate
Begin
Insert #T Select Convert(Varchar(10),@BeginDate,120)
Set @BeginDate=DateAdd(dd,1,@BeginDate)
End
Select * from #T Order By ListDay
End
GO
EXEC ListDay '2005'