-- 1 select count(n) from dbo.Nums where n like '%5%' and n between 9999 and 100000 and n % 2 = 0; -- Odpowiedź : 15840 -- 2 create table #Osoby ( Id int primary key identity(1,1), Imie varchar(50) not null, Nazwisko varchar(50) not null, Data_urodzenia date, Plec varchar(1), Pesel varchar(11) ); --insert into #Osoby values ('Jakub','Salega','data ur','M','twoj pesel'); --insert into #Osoby values ('xyz','xyz','1999-09-06','M','12345678901'); --insert into #Osoby values ('zyx','zyx','2015-01-02','K','12345678901'); CREATE FUNCTION dbo.PeselValidator(@peselnumb nchar(11)) RETURNS bit AS BEGIN IF ISNUMERIC(@peselnumb) = 0 RETURN 0 IF LEN(@peselnumb) != 11 RETURN 0 DECLARE @weights AS TABLE ( Position tinyint IDENTITY(1,1) NOT NULL, Weight tinyint NOT NULL ) INSERT INTO @weights VALUES (1), (3), (7), (9), (1), (3), (7), (9), (1), (3), (1) IF (SELECT SUM(CONVERT(TINYINT, SUBSTRING(@peselnumb, Position, 1)) * Weight) % 10 FROM @weights ) = 0 RETURN 1 RETURN 0 END select * from #Osoby; select Imie, Nazwisko, Pesel from #Osoby where dbo.PeselValidator(Pesel) = 1;