想大家求 15位和18位 身份证查询问题
表中的有身份证字段   里面有15位和18位的号码 
       现在要求输入一个身份证号,不论是15位或者18位都算正确。 
       就是现在有证号633522198412015819 
    现在输入633522841201581   可以查到上面18位的号码。 
    如果输入的是543522198412011234   (18位)可以查到543522841201123   (15位)   这个号码   
       就是不论是15位或者18位都可以查到该数据。。
------解决方案--------------------身份证字段 里面有15位和18位的号码---这两个怎么存在表里的?
------解决方案----------------------統一轉化為15位來處理   
 create proc sp_qryid 
  @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
------解决方案--------------------有两张表,由于输入时间的关系,每张表中的身份证号都存在15位和18位的问题,又由于同名同姓的问题,只能通过身份证号来选出同时在两张表中都存在的人,   
 select from table1 inner join table2 on talbe1.identificationNo=table2.identificationNo   
 这时同一个人如果在两个表中身份证号位数不相同就选不出来,   
 请高手之交,怎样能解决这个问题?????不胜感谢!!   
 ---------- 
 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)))   
 希望这个能帮助你
------解决方案--------------------Select * From TEST  
 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) = @身份证号))) 
------解决方案--------------------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) end
------解决方案--------------------create table T(num varchar(18)) 
 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) end   
 drop table t   
 /* 
 num                 
 ------------------  
 420583198110067614 
 420583811006761 
 */
------解决方案--------------------18位的查15位的,只需要去掉18位号码中第七、八位与最后一位较验码就可以了,反过来15位查18位,只能在第七、八位插入“19”后,比对前17位,最后一位校验位要有算法生成的。
------解决方案--------------------寫一個函數統一處理為15位,再做判斷   
 --創建函數 
 Create Function F_TEST(@身份证号 Varchar(20)) 
 Returns Varchar(20) 
 As 
 Begin 
 	If Len(@身份证号) = 18