我现在有 year,month两个字段,都是int型。怎样可以利用这两个字段拼datetime类型(日期,时间随意多少都可以)
例如: year = 2008;month = 3 把他改成 2008-3-31 10:21:36

解决方案 »

  1.   


    select cast(cast([year] as varchar) + '-' + cast([month] as varchar) + '-01' as datetime) from tb
      

  2.   

    declare @year int,@month int
    set @year=2008
    set @month=3select cast(ltrim(@year)+'-'+ltrim(@month)+'-31 10:21:36' as datetime)2008-03-31 10:21:36.000
      

  3.   

    select ltrim([year])+'-'+ltrim([month])+ right(convert(char(19),getdate(),120),12)
    from ta
      

  4.   

    declare @year int,@month int
    set @year=2008
    set @month=3select ltrim(@year)+'-'+ltrim(@month)+ right(convert(char(19),getdate(),120),12)
    /*
                                                 
    -------------------------------------------- 
    2008-3-31 10:29:37(所影响的行数为 1 行)
    */
      

  5.   

    select convert(datetime,cast(2008 as varchar(4))+'-'+cast(3 as varchar(1))+'-31',120)