Analysis of the date of birth, gender, and age of SQL statements by ID card

Source: Internet
Author: User
Tags getdate rtrim

Update [Table name] Set [Sex]=( Case  when( Left( Right([ID Card],2),1)% 2 = 0) Then 'female' Else 'male' End)where Len([ID Card])= -  and  Left( Right([ID Card],2),1)<>'X'Update [Table name] Set [Sex]=( Case  when( Right([ID Card],1)% 2 = 0) Then 'female' Else 'male' End)where Len([ID Card])= the  and  Right([ID Card],1)<>'X'Update [Table name] Set [Date of birth]=( Right( Left([ID Card],Ten),4)+'-'+right( Left([ID Card], A),2)+'-'+right( Left([ID Card], -),2))where Len([ID Card])= -Update [Table name] Set [Date of birth]=(' +'+right( Left([ID Card],8),2)+'-'+right( Left([ID Card],Ten),2)+'-'+right( Left([ID Card], A),2))where Len([ID Card])= theUpdate [Table name] Set [Age]= Year(getdate())-(cast( Left([Date of birth],4) as int))where [Date of birth]<>"'

#字符串格式化为日期格式SELECTDate_format ('19841201','%y-%m-%d') #---Know the date of birth for ageSELECTTimestampdiff ( Year, Date_format ('19841201','%y-%m-%d'), curdate ()); #截取身份证号中的年月日SELECTSUBSTR ('152824197810116015',7,8);SELECTSUBSTR ('152824781011601',7,6) #连接字符串SELECTCONCAT (' -',' A') #截取身份证号中的年月日1SELECT  IF(LENGTH ('152824197810116015')= -, SUBSTR ('152824197810116015',7,8), CONCAT (' +', SUBSTR ('152824197810116015',7,6)) ; #截取身份证号中的年月日2SELECT  IF(LENGTH ('152824781011601')= -, SUBSTR ('152824781011601',7,8), CONCAT (' +', SUBSTR ('152824781011601',7,6)) ; #根据身份证号计算年龄SELECTTimestampdiff ( Year, Date_format (IF(LENGTH ('152824781011601')= -, SUBSTR ('152824781011601',7,8), CONCAT (' +', SUBSTR ('152824781011601',7,6))                    ),'%y-%m-%d'), curdate ());

Update @TSetnl=  Case  when Len(LTrim(RTrim(SFZ)))=  -  Then cast( Year(GetDate()) as int)- cast(substring(LTrim(RTrim(SFZ)),7,4) as int)  Else cast( Year(GetDate()) as int)- cast(' +'+substring(LTrim(RTrim(SFZ)),7,2) as int)End

Analysis of the date of birth, gender, and age of SQL statements by ID card

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.