如题  SQL语句怎么写SQL SQL

解决方案 »

  1.   


    declare @str nvarchar(20)
    set @str = '2013-04'
    select Convert(datetime,(@str+'-01'), 120)
      

  2.   

    create function generateTime(    @begin_date datetime,    @end_date datetime)returns @t table(date datetime)asbegin    with maco as    (       select @begin_date AS date       union all       select date+1 from maco       where date+1 <=@end_date    )    insert into @t    select * from maco option(maxrecursion 0);    returnend
    DECLARE @1 datetime
    DECLARE @2 datetime
    DECLARE @now varchar(20)
    DECLARE @a int
    SET @a=4
    SET @now='2013-'+CONVERT(CHAR(2),@a)+'-'+'01'
    set @1=DATEADD(MONTH,DATEDIFF(MONTH,0,@now),0)
    set @2=DATEADD(DD,-1,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@now),0)) select * from dbo.generateTime(@1,@2)/*
    date
    -----------------------
    2013-04-01 00:00:00.000
    2013-04-02 00:00:00.000
    2013-04-03 00:00:00.000
    2013-04-04 00:00:00.000
    2013-04-05 00:00:00.000
    2013-04-06 00:00:00.000
    2013-04-07 00:00:00.000
    2013-04-08 00:00:00.000
    2013-04-09 00:00:00.000
    2013-04-10 00:00:00.000
    2013-04-11 00:00:00.000
    2013-04-12 00:00:00.000
    2013-04-13 00:00:00.000
    2013-04-14 00:00:00.000
    2013-04-15 00:00:00.000
    2013-04-16 00:00:00.000
    2013-04-17 00:00:00.000
    2013-04-18 00:00:00.000
    2013-04-19 00:00:00.000
    2013-04-20 00:00:00.000
    2013-04-21 00:00:00.000
    2013-04-22 00:00:00.000
    2013-04-23 00:00:00.000
    2013-04-24 00:00:00.000
    2013-04-25 00:00:00.000
    2013-04-26 00:00:00.000
    2013-04-27 00:00:00.000
    2013-04-28 00:00:00.000
    2013-04-29 00:00:00.000
    2013-04-30 00:00:00.000
    */
      

  3.   

    先创建函数,如果create上方还有语句,就在create上面加一个go
      

  4.   

    select dateadd(d,number,rq)
    from (select convert(datetime,'2013-'+'4'+'-01') rq) a 
    left join 
    (select * from spt_values where type = 'P') b 
    on a.rq=a.rq
    where dateadd(d,number,rq)<DATEADD(m,1,'2013-'+'4'+'-01')
      

  5.   

    上面的有的多余的代码,下面的好一点。
    select dateadd(d,number,rq)
    from (select convert(datetime,'2013-'+'4'+'-01') rq) a 
    ,(select * from spt_values where type = 'P') b 
    where dateadd(d,number,rq)<DATEADD(m,1,'2013-'+'4'+'-01')
      

  6.   


    declare @now varchar(20)
    DECLARE @a int
    SET @a=2  ------这里输入月份
    SET @now='2013-'+CONVERT(CHAR(2),@a)+'-'+'01'
    declare @last nvarchar(100)
    SET @last='2013-'+CONVERT(CHAR(2),@a+1)+'-'+'01'
    ;with tb as (
    select cast(@now as datetime) as date1
    union all
    select date1+1 as date1 from tb where date1<cast( @last as datetime) -1
    )
    select * from tb