create function f_getinfo(@type int,@name varchar(60)) returns varchar(60) as begin declare @i int,@j int declare @r varchar(60)
set @i = 1 set @j = 1
--Get 姓名 if(@type = 1) begin while(@j<len(@name)) begin if(substring(@name,@j,1)='(' or substring(@name,@j,1)='(' or ISNUMERIC(substring(@name,@j,1))=1) break else set @j = @j + 1 end end --Get 证件号码 else begin -- while(@j<len(@name)) begin if(ISNUMERIC(substring(@name,@j,1))!=1) begin set @i = @i + 1 set @j = @j + 1 end else begin set @j = @j + 1 break end end while(@j<len(@name)) begin if(ISNUMERIC(substring(@name,@j,1))=1 and substring(@name,@j,1) !=' ') set @j = @j + 1 else break end end
--建立测试环境 Create Table 表(a varchar(100)) --插入数据 insert into 表 select '张三612345678901234' union select '张三三612345678901234' union select '李四612345678901234567' union select '李四四612345678901234567' union select '王五(护照号)12345678' union select '王五五(护照号)12345678' --测试语句 go CREATE function GetFirstNum(@str varchar(255)) RETURNS varchar(255) as begin declare @i integer, @start integer, @end integer, @len integer, @result char(255) set @i=1 set @start=0 set @end=0 set @result='' while @i<=len(@str) begin if (substring(@str,@i,1)>='0') and (substring(@str,@i,1)<='9') begin if @start=0 set @start=@i end else if @start>0 begin set @end=@i break end Set @i=@i+1 end if @start>0 and @end=0 set @len=len(@str)-@start+1 else set @len=@end-@start if @start>0 set @result=substring(@str,@start,@len) return(@result) end go select replace(a,rtrim(dbo.getfirstnum(a)),'')姓名,dbo.getfirstnum(a) 证件号 from 表
--删除测试环境 Drop Table 表 drop function GetFirstNum /* 李四 612345678901234567 李四四 612345678901234567 王五(护照号) 12345678 王五五(护照号) 12345678 张三 612345678901234 张三三 612345678901234 */
--取姓名 --取证件号码取出证件号然后replace一下变成'',剩下的就是姓名了
select 姓名=left(str,patindex('%[0-9]%',@str+'0')-1) ,证件号=stuff(str,1,patindex('%[0-9]%',@str+'0')-1,'') from( -- 这后面是直接写测试数据,你可以直接写表名 select str=N'张三612345678901234' union all select str=N'张三三612345678901234' union all select str=N'李四612345678901234567' union all select str=N'李四四612345678901234567' union all select str=N'王五(护照号)12345678' union all select str=N'王五五(护照号)12345678' )a
写个函数做个循环
用substring 一个字接一个字符的取出来
用datalength判断这个字符的长度是不是1
是1的话再判断是不是 )
记录这个位置
用right之类的取这个位置后面的字符串
returns varchar(60)
as
begin
declare @i int,@j int
declare @r varchar(60)
set @i = 1
set @j = 1
--Get 姓名
if(@type = 1)
begin
while(@j<len(@name))
begin
if(substring(@name,@j,1)='(' or substring(@name,@j,1)='(' or ISNUMERIC(substring(@name,@j,1))=1)
break
else
set @j = @j + 1
end
end
--Get 证件号码
else
begin
--
while(@j<len(@name))
begin
if(ISNUMERIC(substring(@name,@j,1))!=1)
begin
set @i = @i + 1
set @j = @j + 1
end
else
begin
set @j = @j + 1
break
end
end
while(@j<len(@name))
begin
if(ISNUMERIC(substring(@name,@j,1))=1 and substring(@name,@j,1) !=' ')
set @j = @j + 1
else
break
end
end
return substring(@name,@i,@j-@i)
end
--取姓名
select dbo.f_getinfo(1,'张三612345678901234 (十五位身份证号码)')
--取证件号码
select dbo.f_getinfo(2,'张三612345678901234 (十五位身份证号码)')
Create Table 表(a varchar(100))
--插入数据
insert into 表
select '张三612345678901234' union
select '张三三612345678901234' union
select '李四612345678901234567' union
select '李四四612345678901234567' union
select '王五(护照号)12345678' union
select '王五五(护照号)12345678'
--测试语句
go
CREATE function GetFirstNum(@str varchar(255))
RETURNS varchar(255)
as
begin
declare @i integer,
@start integer,
@end integer,
@len integer,
@result char(255)
set @i=1
set @start=0
set @end=0
set @result=''
while @i<=len(@str)
begin
if (substring(@str,@i,1)>='0') and (substring(@str,@i,1)<='9')
begin
if @start=0 set @start=@i
end
else
if @start>0
begin
set @end=@i
break
end
Set @i=@i+1
end
if @start>0 and @end=0
set @len=len(@str)-@start+1
else
set @len=@end-@start
if @start>0 set @result=substring(@str,@start,@len)
return(@result)
end
go
select replace(a,rtrim(dbo.getfirstnum(a)),'')姓名,dbo.getfirstnum(a) 证件号 from 表
--删除测试环境
Drop Table 表
drop function GetFirstNum
/*
李四 612345678901234567
李四四 612345678901234567
王五(护照号) 12345678
王五五(护照号) 12345678
张三 612345678901234
张三三 612345678901234 */
--取证件号码取出证件号然后replace一下变成'',剩下的就是姓名了
,证件号=stuff(str,1,patindex('%[0-9]%',@str+'0')-1,'')
from( -- 这后面是直接写测试数据,你可以直接写表名
select str=N'张三612345678901234' union all
select str=N'张三三612345678901234' union all
select str=N'李四612345678901234567' union all
select str=N'李四四612345678901234567' union all
select str=N'王五(护照号)12345678' union all
select str=N'王五五(护照号)12345678'
)a
select 姓名=left(str,patindex('%[0123456789(]%',@str+'0')-1)
,证件号=stuff(str,1,patindex('%[0123456789(]%',@str+'0')-1,'')
fromTigerSuper(菜鸟(鸟吃菜吗?)) 的思路符合我的想法,但是我没有写出来,那位哥们,帮帮忙,我开新贴送分