Situatie
Pentru cazurile in care avem nevoie intr-o aplicatie (sau extern unei aplicatii) de validarea unui CNP, am facut o procedura ce face acest lucru. Procedura primeste ca parametru un varchar @CNP si, in cazul in care este valid , procedura returneaza 1, iar in cazul in care este invalid procedura returneaza 0.
Solutie
create proc BD_VerificareCNP
@CNP varchar(20)
as
if datalength(@CNP) <> 13
GOTO WRONG
declare
@Pos int,
@Digit char(1),
@DigitC char(1),
@Control char(13),
@SumaC int
select
@Control = ‘279146358279’,
@SumaC = 0,
@Pos = 1
while @Pos <= 13
begin
select @Digit = substring(@CNP,@Pos,1)
select @DigitC = substring(@Control,@Pos,1)
if isnumeric(@Digit) = 0
GOTO WRONG
select @SumaC = @SumaC + convert(int,@Digit)*convert(int,@DigitC)
select @Pos = @Pos+1
end
–SELECT @SumaC
–SELECT @SumaC%11
–SELECT @Digit
DECLARE @SumaC_Final int
set @SumaC_Final = @SumaC%11
if @SumaC_Final = 10 set @SumaC_Final = 1
if @SumaC_Final = convert(int,@Digit)
GOTO OK
WRONG:
select ‘IsOK’ = 0
return 0
OK:
select ‘IsOK’ = 1
Leave A Comment?