日期:2014-05-18  浏览次数:20529 次

写一个列举一月内所有日期的视图
求各位帮忙

------解决方案--------------------
SQL code

--定义变量,赋值为当前日期
declare @t datetime set @t=getdate()

select distinct datecol from tablename where 
convert(varchar(7),datecol,120)=
convert(varchar(7),@t,120)

------解决方案--------------------
SQL code
create view GetdtView
as
with Getdt
as
 (
 select cast(convert(varchar(7),getdate(),120)+'-01' as datetime) dt
 union all
 select dateadd(day,1,dt) 
 from Getdt
 where dt<dateadd(day,-1,convert(char(07),dateadd(month,1,getdate()),120)+'-01')
 )
select * from Getdt

------解决方案--------------------
select dateadd(day,number,convert(varchar(7),DATEADD(MONTH,-1,GETDATE()),120)+'-01')dt
from master..spt_values
where type='P' and number<day(dateadd(day,-1,convert(char(07),dateadd(month,1,DATEADD(MONTH,-1,GETDATE())),120)+'-01'))

------解决方案--------------------
SQL code

create view VwGetDate
as
 select dateadd(dd,number,DATEADD(m,-1,getdate())) AS date 

    from master..spt_values 

    where type='p' and dateadd(dd,number,DATEADD(m,-1,getdate()))<=GETDATE()