我现在一个需求,用户表中一个字段身份证号码(真实的,18位),然后取出每个人身份证号码,然后获取通过身份证获取他们年龄,生日,性别。然后把这些信息再写到用户表中。如何操作。
UserTBID     CID         sex     age     bday
1     身份号码     性别    年龄   生日
现在写一个存储过程,先取出用户身证号码,然后进行处理,把性别,年龄,生日再写回这个用户。这个存储我不会,还请大家指教。身份证号码获取性别,年龄,生日,这个我会写,就是前面如何读取数据和如何写数据。怎么写。

解决方案 »

  1.   


    create table tb(cid nvarchar(18),sex char(4),age int,birthday datetime)insert tb values ('44568119850903051X',null,null,null)
    insert tb values ('44568119831213151X',null,null,null)update tb set sex=t.sex,age=t.age,birthday=t.birthday
    from
    (
    select cid,convert(datetime,substring(cid,7,8)) birthday,datediff(year,convert(datetime,substring(cid,7,8)),getdate()) age,
    (case when (substring(cid,15,1))='0' then '男'
    else '女' end) sex from tb) t
    where t.cid=tb.cid/*
    44568119850903051X 男   25 1985-09-03 00:00:00.000
    44568119831213151X 女   27 1983-12-13 00:00:00.000
    */
      

  2.   


    declare @str nvarchar(50)
    set @str='371323199001244831'
    select cast(SUBSTRING(@str,7,8) as DATE) as [bday],DATEDIFF(YEAR,cast(SUBSTRING(@str,7,8) as DATE),cast(GETDATE() as DATE)) as [age],
           case cast(left(RIGHT(@str,2),1) as int)%2 when 0 then '女' else '男' end as [sex]bday age sex
    1990-01-24 20 男