用sql语言更新日期,例如:1907-9-8和1806-8-7更新为 2007-9-8 和2006-8-7
我 写的sql语句提示(附近语法错误!希望改正下,谢谢!
update info set substring(cast(year(graduation_date) as varchar),1,2)='20'where year(graduation_date) not like '20%'

解决方案 »

  1.   


    update tb set col = '200' + right(convert(varchar(10),col,120),7)
      

  2.   

    create table tb(col datetime)
    insert into tb values('1907-9-8')
    insert into tb values('1806-8-7')
    goupdate tb set col = '200' + right(convert(varchar(10),col,120),7)select * from tbdrop table tb/*
    col                                                    
    ------------------------------------------------------ 
    2007-09-08 00:00:00.000
    2006-08-07 00:00:00.000(所影响的行数为 2 行)
    */
      

  3.   

    update tb set fdate=dateadd(yy,1907-1806,fdate) where fdate='1907-9-8'
    如果更新的内容不多,可以直接更新。
    update tb set fdate='2007-9-8' where fdate='1907-9-8'另一个同理。
      

  4.   


    只能替换字段的部分内容,不能直接修改某部分内容
    在你的基础上改一下:update info 
    set graduation_date=replace(graduation_date,
                                substring(cast(year(graduation_date) as varchar),1,2),
                                '20') 
    where year(graduation_date) not like '20%'
      

  5.   

    如果字段是字符型,可以这样(日期型的楼上代码有了)create table #tb(col varchar(10))
    insert into #tb values('1907-9-8')
    insert into #tb values('1806-8-7')
    goupdate #tb set col = '20' + substring(col,3,10)select * from #tbdrop table #tb