表中的有身份证字段 里面有15位和18位的号码
现在要求输入一个身份证号,不论是15位或者18位都算正确。
就是现在有证号633522198412015819
现在输入633522841201581 可以查到上面18位的号码。
如果输入的是543522198412011234 (18位)可以查到543522841201123 (15位) 这个号码 就是不论是15位或者18位都可以查到该数据。。
现在要求输入一个身份证号,不论是15位或者18位都算正确。
就是现在有证号633522198412015819
现在输入633522841201581 可以查到上面18位的号码。
如果输入的是543522198412011234 (18位)可以查到543522841201123 (15位) 这个号码 就是不论是15位或者18位都可以查到该数据。。
@id varchar(18)
as
begin
select @id = case len(@id)
when 15 then @id
when 18 then left(@id, 6) + substring(@id, 9, 9)
end select * from 表 where @id in
(select id = case len(身份證字段)
when 15 then 身份證字段
when 18 then left(身份證字段, 6) + substring(身份證字段, 9, 9)
end
from 表)
end
select a.name from table1 a,table2 b where a.identificationNo=b.identificationNo or (substring(a.identificationNo,1,6)=substring(b.identificationNo,1,6) and (substring(a.identificationNo,9,8)=substring(b.identificationNo,11,8) or substring(a.identificationNo,11,8)=substring(b.identificationNo,9,8)))希望这个能帮助你
Where (Len(@身份证号) = 18
And ((Len(身份证号) = 18 And 身份证号 = @身份证号) Or (Len(身份证号) = 15 And 身份证号 = Left(Stuff(@身份证号, 7, 2, ''), 15))))
Or
(Len(@身份证号) = 15
And ((Len(身份证号) = 15 And 身份证号 = @身份证号) Or (Len(身份证号) = 18 And Left(Stuff(身份证号, 7, 2, ''), 15) = @身份证号)))
case when len(@input)=15 then @input when len(@input)=18 then left(stuff(@input,7,2,''),15) end
insert into T
select '420583198110067614'
insert into T
select '420583198110067625'
insert into T
select '420583198110067636'
insert into T
select '420583811006761'
insert into T
select '420583811006762'
declare @input varchar(18)
set @input='420583811006761'
select * from T
where case when len(num)=15 then num when len(num)=18 then left(stuff(num,7,2,''),15) end=
case when len(@input)=15 then @input when len(@input)=18 then left(stuff(@input,7,2,''),15) enddrop table t/*
num
------------------
420583198110067614
420583811006761
*/
Create Function F_TEST(@身份证号 Varchar(20))
Returns Varchar(20)
As
Begin
If Len(@身份证号) = 18
Select @身份证号 = Left(Stuff(@身份证号, 7, 2, ''), 15)
Return @身份证号
End
GO
--測試
Select * From TEST Where dbo.F_TEST(身份证号) = dbo.F_TEST('633522841201581')
Select * From TEST Where dbo.F_TEST(身份证号) = dbo.F_TEST('543522198412011234')
GO
Create Table TEST(身份证号 Varchar(20))
Insert TEST Select '633522198412015819'
Union Select '543522841201123'
GO
--創建函數
Create Function F_TEST(@身份证号 Varchar(20))
Returns Varchar(20)
As
Begin
If Len(@身份证号) = 18
Select @身份证号 = Left(Stuff(@身份证号, 7, 2, ''), 15)
Return @身份证号
End
GO
--測試
Select * From TEST Where dbo.F_TEST(身份证号) = dbo.F_TEST('633522841201581')
Select * From TEST Where dbo.F_TEST(身份证号) = dbo.F_TEST('543522198412011234')
GO
--刪除測試環境
Drop Table TEST
Drop Function F_TEST
--結果
/*
身份证号
633522198412015819身份证号
543522841201123
*/
returns bit
as
begin
if len(@sno)!=15 and len(@sno)!=18
return 0
if len(@dno)!=15 and len(@dno)!=18
return 0
if @dno=@sno
return 1
if len(@dno)=len(@sno)
return 0
if len(@sno)=15
begin
if @sno=substring(@dno,1,6)+substring(@dno,9,9)
return 1
end
else
begin
if @dno=substring(@sno,1,6)+substring(@sno,9,9)
return 1
end
return 1
endselect * from 表 where dbo.cmpno(身份证字段,@查询数据)=1
returns bit
as
begin
if len(@sno)!=15 and len(@sno)!=18
return 0
if len(@dno)!=15 and len(@dno)!=18
return 0
if @dno=@sno
return 1
if len(@dno)=len(@sno)
return 0
if len(@sno)=15
begin
if @sno=substring(@dno,1,6)+substring(@dno,9,9)
return 1
end
else
begin
if @dno=substring(@sno,1,6)+substring(@sno,9,9)
return 1
end
return 0
end现在高手回复真快,我们菜鸟们想混点分都难
最后一位是校验位所以18位转成15位后正确的身份证号有可能是不正确的
也就是说转成15位判断的方法不可取,所以必须有15转18的函数
以前写过,不知道放哪里了-----------------------------??18的,去掉中間的'19',然後將最後的校驗位去掉,不就是15了。那個校驗位應該不影響吧。PS:15位轉18位的函數,我的BLOG上有。http://blog.csdn.net/paoluo/archive/2006/04/07/653860.aspx記得好象是紅塵寫的。