可以参考这个:
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
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
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'
As
Begin
Declare @BeginDate DateTime,@EndDate DateTime ,@intval
Set @BeginDate=@year+'-01-01'
Set @EndDate=@year+'-12-31'
set @intval = datediff(day,@begindate,@enddate)
select identity(int,1,1) into #t from sysobjects a,sysobjects b,sysobjects c
select dateadd(day,@begindate,a.id) from #t a where a.id<=@intval
drop table #t
End
GO
EXEC ListDay '2005'
As
Begin
Declare @BeginDate DateTime,@EndDate DateTime ,@intval int
Set @BeginDate=@year+'-01-01'
Set @EndDate=@year+'-12-31'
set @intval = datediff(day,@BeginDate,@EndDate)
select [id]=identity(int,0,1) into #t from sysobjects
select convert(char(10),dateadd(day,a.id,@BeginDate),120) from #t a where a.id<=@intval
drop table #t
End
GO
EXEC ListDay '2005'
@sdt datetime,
@edt datetime,
@num int,
@loop int
set @year = 2004
set @sdt = convert(datetime,@year+'-01-01',120)
set @edt = convert(datetime,convert(varchar(4),convert(int,@year)+1)+'-01-01',120)
set @num = datediff(day,@sdt,@edt)
set @loop = 0
create table #t(d datetime)
while @loop < @num
begin
insert into #t
select d = dateadd(day,@loop,@sdt)
set @loop = @loop + 1
end
select * from #t
drop table #t
if object_id('ListDay') is not null drop proc ListDay
go
Create ProceDure ListDay(@year char(4))
As
Begin --得到月份序列1~12和日序列1~30,再进行笛卡尔积,再去掉一些不必要的日期即得到结果
declare @yy int
select mid=identity(int) into #month from sysobjects
select did=identity(int) into #day from sysobjects
delete #month where mid>12
delete #day where did>31
select convert(varchar(2), mid)+'-'+convert(varchar(2), did) as md
into #result
from #month, #day
delete #result
where md in(
'2-31', '2-30',
'4-31', '6-31', '9-31','11-31')
set @yy=convert(int, @year) --处理非闰年的情况
if not(@yy%400=0)or(@yy%4=0 and @yy%100<>0) delete #result where md='2-29'
select @year+'-'+md as yymmdd
from #result
order by convert(datetime, @year+'-'+md)
drop table #month
drop table #day
drop table #result
End
GO
EXEC ListDay '2005'