有一个表,由于当初设计的时候程序部分控制不完美,结果某一个字段data 格式是varchar的,里面保存时间时间格式很乱 有2008-8-7、2008-08-7、2008-8-07 等都有现在我需要把他统一格式成2008-08-07格式的求个语句?

解决方案 »

  1.   

    如果都是日期型字符(即合法的)
    直接改数据类类型为datetime就好了
      

  2.   

    select convert(nvarchar(10),cast('2008-8-7' as datetime) ,120)
    select convert(nvarchar(10),cast(data as datetime) ,120)
      

  3.   

    update tb
    set 日期=convert(varchar(10),(cast(日期 as datetime)),120)
    where isdate(日期)=1
      

  4.   


    update tb set col=case when isdate(col)=1 then convert(varchar(10),cast (col as datetime),120) else col end
    update tb set col=convert(varchar(10),cast (col as datetime),120) 
    where isdate(col)=1
      

  5.   

    update tb
    set 日期=convert(varchar(10),(cast(日期 as smalldatetime)),120)
    where isdate(日期)=1