表中的有身份证字段 里面有15位和18位的号码
  现在要求输入一个身份证号,不论是15位或者18位都算正确。
  就是现在有证号633522198412015819
 现在输入633522841201581 可以查到上面18位的号码。
 如果输入的是543522198412011234 (18位)可以查到543522841201123 (15位) 这个号码  就是不论是15位或者18位都可以查到该数据。。

解决方案 »

  1.   

    --統一轉化為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
      

  2.   

    有两张表,由于输入时间的关系,每张表中的身份证号都存在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)))希望这个能帮助你
      

  3.   

    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) = @身份证号)))
      

  4.   

    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
      

  5.   

    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) enddrop table t/*
    num                
    ------------------ 
    420583198110067614
    420583811006761
    */
      

  6.   

    寫一個函數統一處理為15位,再做判斷--創建函數
    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
      

  7.   

    --創建測試環境
    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
    */
      

  8.   

    alter function cmpno (@sno varchar(18),@dno varchar(18))
    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
      

  9.   

    create function cmpno (@sno varchar(18),@dno varchar(18))
    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现在高手回复真快,我们菜鸟们想混点分都难
      

  10.   

    Haiwer(海阔天空) ( ) 信誉:138  2007-07-26 15:50:01  得分: 0  
     
     
       最后一位是校验位所以18位转成15位后正确的身份证号有可能是不正确的
    也就是说转成15位判断的方法不可取,所以必须有15转18的函数
    以前写过,不知道放哪里了-----------------------------??18的,去掉中間的'19',然後將最後的校驗位去掉,不就是15了。那個校驗位應該不影響吧。PS:15位轉18位的函數,我的BLOG上有。http://blog.csdn.net/paoluo/archive/2006/04/07/653860.aspx記得好象是紅塵寫的。