通过身份证号获得年龄

sqlserver

if len(@idcard) = 15 OR len(@idcard) = 18 
	begin
		set @var1=''
		if LEN(@idcard)=15
			begin
				set @var1 =SUBSTRING(@idcard,15,1)
				set @birthday='19' +SUBSTRING(@idcard,7,2)+'-' +SUBSTRING(@idcard,9,2) +'-' +SUBSTRING(@idcard,11,2)
			end
		else
			begin
				set @var1 =SUBSTRING(@idcard,17,1)
				set @birthday=SUBSTRING(@idcard,7,4) +'-' +SUBSTRING(@idcard,11,2) +'-' +SUBSTRING(@idcard,13,2)
			end

		set @age= round (( DATEDIFF(day,@birthday,getdate())/ 365),0)
	end

Oracle
 

if length(vcardid) = 15 OR length(vcardid) = 18 then
	begin
		if length(vcardid) = 18 then
			begin
				select TRUNC ((  sysdate- TO_DATE (SUBSTR (vcardid, 7, 8), 'yyyy-mm-dd') )/ 365) into v_card_nl from dual;
                select SUBSTR (vcardid, 7, 8)  into V_csrq from dual;--出生日期
			end;
		else
			begin
				select TRUNC ((  sysdate- TO_DATE ('19' || SUBSTR (vcardid, 7, 6), 'yyyy-mm-dd') )/ 365) into v_card_nl from dual;
			end;
		end if;
	end;
end if;

猜你喜欢

转载自blog.csdn.net/sqylqq/article/details/110110683