我在SQL Server数据库中有一字段csrq,其内容有1/12/1988;10/11/1986;1/8/1986等表示日/月/年,我想用一个SQL语句将其格式全都修改为1986-12-01;1986-11-10格式,
请问Sql语句该怎么写?

解决方案 »

  1.   

    declare @m varchar(10)
    set @m='10/11/1986'select cast(substring(@m,7,4)+'-'+substring(@m,1,2)+'-'+substring(@m,4,2) as datetime)
      

  2.   

    select convert(datetime,'1/12/1988',120)
    /*
                                                           
    ------------------------------------------------------ 
    1988-01-12 00:00:00.000*/
      

  3.   

    declare @t table([date] varchar(10))
    insert @t select '1/12/1988'
    insert @t select '10/11/1986'
    select 
    right([date],4)+'-'+substring([date],charindex('/',[date])+1,charindex('/',[date],charindex('/',[date])+1)-charindex('/',[date])-1)
    +'-'+left([date],charindex('/',[date])-1) from @t
    /*
                                   
    ------------------------------ 
    1988-12-1
    1986-11-10*/
      

  4.   

    update tb  set csrq=convert(datetime,csrq,120)
      

  5.   


    select convert(varchar(10),convert(datetime,'1/12/1988',120),120)---------- 
    1988-01-12(所影响的行数为 1 行)
      

  6.   

    select csrq=convert(datetime,csrq,20) from tb 
      

  7.   

    我将数据按您的程序转换过来了,发现我先前理解错了,格式定义是:月/日/年,现在转换成的数据变为了:年-日-月了,我想写个SQL语句将它改为:年-月-日,请教啊!急!
      

  8.   


    select right(RTRIM(csrq),4)+'-'+left(ltrim(a),CHARINDEX('/', csrq, 1)-1)+'-'+substring(RTRIM(STUFF(csrq, 1, CHARINDEX('/', csrq, 1), '')),0,3) from table
      

  9.   

    这个还不简单
     SELECT CONVERT(datetime,'11/1/2003',103)
    保证正确.
      

  10.   

    这个还不简单
     SELECT CONVERT(datetime,'11/1/2008',103)
    保证正确.