sql server 2000数据库
表table1里的日期字段为字符型,格式为yyyy-m-d,记录已经有好多条了,请问怎样把它们改为yyyy-mm-dd格式

解决方案 »

  1.   

    如果sql 编程,就用substring,charindex,stuff,len之类分析日/月的长度,然后替换.如果再DELPHI下做,你把日期读出,根据'-'分割符拆分字符串(可以利用Tstringlist),然后判断长度后,进行相应操作.
      

  2.   

    用游标给你写了一个declare @m char(2)
    declare @d char(2)
    declare @rq char(10)
    declare @y char(4)declare ctm cursor
    for 
    select substring(value,1,4),substring(value,6,len(value)- charindex('-', REVERSE(value))-5),
    substring(value,len(value)- charindex('-', REVERSE(value))+2,2)
    from testtopen ctmfetch next from ctm into @y,@m,@d
    while (@@fetch_status=0) 
    begin
      if (len(@m)=1 ) begin
       set @m = '0'+@m
      end
      if (len(@d)=1) begin
       set @d = '0'+@d
      end
      set @rq= @y+'-'+@m+'-'+@d 
      print @rq
      update testt 
      set value=@rq  where current of ctm
      fetch next from ctm into @y,@m,@d
    end
    close ctm
    deallocate ctm
      

  3.   

    很好,只是还有一点给忘了:日期后面还有时间,我不想动时间,就是:
    以前的为:yyyy-m-d hh:mm:dd
    以后的为:yyyy-mm-dd hh:mm:dd
      

  4.   

    update table1 set rq = convert(varchar(20),convert(smalldatetime,rq),120) from table1