34,590
社区成员
发帖
与我相关
我的任务
分享
create table tb(身份证 varchar(18), 年龄 int,性别 varchar(2))
insert into tb values('310228600912221' , NULL ,NULL)
insert into tb values('310106195403162428', NULL,NULL)
insert into tb values('31022419760414562X', 33 ,NULL)
insert into tb values('310226661223491' , NULL ,NULL)
insert into tb values('310228800912231' , NULL ,NULL)
insert into tb values('310228194701013815', NULL ,NULL)
go
--查询
select 身份证 ,
年龄 = case when len(身份证) = 18 and substring(身份证,11,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int)
when len(身份证) = 18 and substring(身份证,11,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int) - 1
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) <= '99' and substring(身份证,9,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('19'+substring(身份证,7,2) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) >= '00' and substring(身份证,9,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('20'+substring(身份证,7,2) as int) - 1
end,
性别 = (case when cast(substring(reverse(身份证),2,1) as int)%2 = 1 then '男' else '女' end)
from tb
/*
身份证 年龄 性别
------------------ ----------- ----
310228600912221 49 女
310106195403162428 55 女
31022419760414562X 33 女
310226661223491 43 男
310228800912231 29 男
310228194701013815 62 男
(所影响的行数为 6 行)
*/
--更新
update tb set
年龄 = case when len(身份证) = 18 and substring(身份证,11,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int)
when len(身份证) = 18 and substring(身份证,11,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int) - 1
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) <= '99' and substring(身份证,9,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('19'+substring(身份证,7,2) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) >= '00' and substring(身份证,9,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('20'+substring(身份证,7,2) as int) - 1
end,
性别 = (case when cast(substring(reverse(身份证),2,1) as int)%2 = 1 then '男' else '女' end)
from tb
select * from tb
/*
身份证 年龄 性别
------------------ ----------- ----
310228600912221 49 女
310106195403162428 55 女
31022419760414562X 33 女
310226661223491 43 男
310228800912231 29 男
310228194701013815 62 男
(所影响的行数为 6 行)
*/
drop table tb
--年龄的
update
tb
set
年龄=(case when len(身份证)=15 then datediff(yy,convert(varchar(10),'19'+cast(substring(身份证,7,6) as varchar(10),120),getdate())
when len(身份证)=15 then datediff(yy,convert(varchar(10),ltrim(substring(身份证,7,8)),getdate())
end
create table tb(身份证 varchar(18), 年龄 int)
insert into tb values('310228600912221' , NULL )
insert into tb values('310106195403162428', NULL)
insert into tb values('284771578' , NULL )
insert into tb values('31022419760414562X', 33 )
insert into tb values('310226661223491' , NULL )
insert into tb values('310228800912231' , NULL )
insert into tb values('310228194701013815', NULL )
go
--查询
select 身份证 ,
年龄 = case when len(身份证) = 18 and substring(身份证,11,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int)
when len(身份证) = 18 and substring(身份证,11,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int) - 1
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) <= '99' and substring(身份证,9,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('19'+substring(身份证,7,2) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) >= '00' and substring(身份证,9,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('20'+substring(身份证,7,2) as int) - 1
end
from tb
/*
身份证 年龄
------------------ -----------
310228600912221 49
310106195403162428 55
284771578 NULL
31022419760414562X 33
310226661223491 43
310228800912231 29
310228194701013815 62
(所影响的行数为 7 行)
*/
--更新
update tb set
年龄 = case when len(身份证) = 18 and substring(身份证,11,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int)
when len(身份证) = 18 and substring(身份证,11,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast(substring(身份证,7,4) as int) - 1
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) <= '99' and substring(身份证,9,4) <= right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('19'+substring(身份证,7,2) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) >= '00' and substring(身份证,9,4) > right(convert(varchar(8),getdate(),112),4) then year(getdate()) - cast('20'+substring(身份证,7,2) as int) - 1
end
from tb
select * from tb
/*
身份证 年龄
------------------ -----------
310228600912221 49
310106195403162428 55
284771578 NULL
31022419760414562X 33
310226661223491 43
310228800912231 29
310228194701013815 62
(所影响的行数为 7 行)
*/
drop table tb
create table tb(身份证 varchar(18), 年龄 int)
insert into tb values('310228600912221' , NULL )
insert into tb values('310106195403162428', NULL)
insert into tb values('284771578' , NULL )
insert into tb values('31022419760414562X', 33 )
insert into tb values('310226661223491' , NULL )
insert into tb values('310228800912231' , NULL )
insert into tb values('310228194701013815', NULL )
go
--查询
select 身份证 ,
年龄 = case when len(身份证) = 18 then year(getdate()) - cast(substring(身份证,7,4) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) <= '99' then year(getdate()) - cast('19'+substring(身份证,7,2) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) >= '00' then year(getdate()) - cast('20'+substring(身份证,7,2) as int)
end
from tb
/*
身份证 年龄
------------------ -----------
310228600912221 49
310106195403162428 55
284771578 NULL
31022419760414562X 33
310226661223491 43
310228800912231 29
310228194701013815 62
(所影响的行数为 7 行)
*/
--更新
update tb set
年龄 = case when len(身份证) = 18 then year(getdate()) - cast(substring(身份证,7,4) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) <= '99' then year(getdate()) - cast('19'+substring(身份证,7,2) as int)
when len(身份证) = 15 and cast(substring(身份证,7,2) as int) >= '00' then year(getdate()) - cast('20'+substring(身份证,7,2) as int)
end
from tb
select * from tb
/*
身份证 年龄
------------------ -----------
310228600912221 49
310106195403162428 55
284771578 NULL
31022419760414562X 33
310226661223491 43
310228800912231 29
310228194701013815 62
(所影响的行数为 7 行)
*/
drop table tb
update
tb
set
性别=(case when left(right(身份证,2),1)%2=1 then '男' else '女' end)