/**//* Description: Get age from id_card_no Return: age Demo: select dbo.fn_get_age_from_card_no('420621198303064511','2008') select dbo.fn_get_age_from_card_no('420621198303064511',default) ---传default */ createfunction[dbo].[fn_get_age_from_card_no] (@card_novarchar(50) --身份证号 ,@yearint=2008--当前年份 ) returnsvarchar(10) as begin declare@rvarchar(10) set@r= @year-left(casewhenlen(@card_no)=15 then'19'+replace(replace(substring(@card_no,7,6),'.',''),',','') whenlen(@card_no)=18 thenreplace(replace(substring(@card_no,7,8),'.',''),',','') else-1end,4) return (casewhen@r=@year+1then'Unknown'else@rend) end