if(15位)
substring('xxxx',7,6)
if(18位)
substring('xxxx',7,8)
else(其他)

解决方案 »

  1.   

    SQL中的字串截取函数是substring()
    SUBSTRING ( expression , start , length ) 参数
    expression
    是字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。
    start
    是一个整数,指定子串的开始位置。
    length
    是一个整数,指定子串的长度(要返回的字符数或字节数)。
      

  2.   

    同意楼上的,通过substring函数截取后,要通过covert转换成想要的日期格式。
      

  3.   

    给个参数不就行了么
    if(@weishu=15)
      

  4.   

    或者
    LEN
    返回给定字符串表达式的字符(而不是字节)个数,其中不包含尾随空格。语法
    LEN ( string_expression ) 参数
    string_expression要计算的字符串表达式。返回类型
    int
      

  5.   

    if(len('xxxx')=15)
       substring('xxxx',7,6)
    else if(len('xxxx')=18)
       substring('xxxx',7,8)
    else
      

  6.   


    有一张表
    出生日期     身份证号
    null         510122198912082373(18位)
    null         5101228912082373(15位)
    我要把出生日期提出来,这个update语句怎么写?
    update ××
    set 出生日期=substring(身份证号,7,8)
    这个就没有判断身份证的位数,15位的就会出现问题。
    这个应该怎么解决
      

  7.   

    update yourtable set 出生日期=case len(身份证号) when 18 then substring(身份证号,7,8)
    when 15 then substring(身份证号,7,6)
      

  8.   

    update table set 出生日期=
    case when len(rtrim(身份证号))=15 then substring(身份证号,7,6)
    when len(rtrim(身份证号))=18 then substring(身份证号,7,8) end如果出生日期是datetime类型,再转换一下
      

  9.   

    miss a word: end------
    update yourtable set 出生日期=case len(身份证号) when 18 then substring(身份证号,7,8)
    when 15 then substring(身份证号,7,6) end
      

  10.   

    update table set 出生日期=
    case when len(rtrim(身份证号))=15 then '年份'+substring(身份证号,7,6)
    when len(rtrim(身份证号))=18 then substring(身份证号,7,8) end
      

  11.   


    CREATE  function Get_Birthday_date
    (@idcard varchar(18))
    returns datetime
    --returns varchar(10)
    as
    begin
    declare @Idlen numeric(2,0),
     @birth varchar(10)
    set @idlen=len(@idcard)
    If @idlen=15 and SUBSTRING(@idcard,7,2)>0 and SUBSTRING(@idcard,9,2)< 13 and 
    SUBSTRING(@idcard,11,2) <32
     
    set @birth='19'+SUBSTRING(@idcard,7,2)+'-'+SUBSTRING(@idcard,9,2)+'-'+SUBSTRING(@idcard,11,2)if @idlen=18 and SUBSTRING(@idcard,11,2) < 13 and SUBSTRING(@idcard,13,2)< 32
    and SUBSTRING(@idcard,7,4)> 0set @birth=SUBSTRING(@idcard,7,4)+'-'+SUBSTRING(@idcard,11,2)+'-'+SUBSTRING(@idcard,13,2)
    If len(@birth) <> 10 
    set @birth='1900-01-01'
    return cast( convert(char(10),@birth)  as datetime)
    --return  @birth 
    end