求问如何将字符‘091201’转换为日期格式‘2009-12-01’

解决方案 »

  1.   

    直接转不行?convert(datetime,'091201')
      

  2.   

    select convert(varchar(10),cast('20'+'091201' as datetime),120)/**
               
    ---------- 
    2009-12-01(所影响的行数为 1 行)
    **/
      

  3.   

    select convert(varchar(10),cast('091201' as datetime),120)
    这样也是可以的
      

  4.   

    select cast('091201' as datetime)
      

  5.   

    select convert(varchar(10),cast('091201' as datetime),120)
      

  6.   

    select convert(varchar(10),convert(datetime,'091201'),120)
    ----------
    2009-12-01
      

  7.   

    select substring(convert(varchar(20),convert(datetime,'091201'),120),0,11)
      

  8.   

    select convert(varchar(10),cast(left(fbatchno,6) as datetime),120) from icinvinitial where left(fbatchno,2)='09' and len(fbatchno)=6以上是我的测试语句,出现错误
      

  9.   

    試下能不能撈到資料select left(fbatchno,6)  from icinvinitial 
    where left(fbatchno,2)='09' 
    and len(fbatchno)=6 
    and isdate(left(fbatchno,6))=1
      

  10.   


    select left(fbatchno,6)  from icinvinitial
    where left(fbatchno,2)='09'
    and len(fbatchno)=6
    and isdate(left(fbatchno,6))=0  --用0,看有沒有資料
      

  11.   

    select  convert(varchar(10),cast(left(fbatchno,6) as datetime),120)  from icinvinitial where left(fbatchno,2)='09' and len(fbatchno)=6 and isdate(left(fbatchno,6))=0  --用0,看有沒有資料
    服务器: 消息 242,级别 16,状态 3,行 5
    从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。
      

  12.   

    select  convert(varchar(10),cast(left(fbatchno,4)+'01' as datetime),120)  from icinvinitial where left(fbatchno,2)='09' and len(fbatchno)=6 and isdate(left(fbatchno,6))=1 测试成功,结贴