declare @ip varchar(15) select @ip = '2.1.232.22'
if ( @ip like replicate('[0-9]', len(substring(@ip, 1, charindex('.', @ip) - 1))) + '.' + replicate('[0-9]', len(substring(@ip, charindex('.', @ip) + 1, charindex('.', @ip, charindex('.', @ip) + 1) - charindex('.', @ip) - 1))) + '.' + replicate('[0-9]', len(reverse(substring(reverse(@ip), charindex('.', reverse(@ip)) + 1, charindex('.', reverse(@ip), charindex('.', reverse(@ip)) + 1) - charindex('.', reverse(@ip)) - 1)))) + '.' + replicate('[0-9]', len(reverse(substring(reverse(@ip), 1, charindex('.', reverse(@ip)) - 1)))) ) and nullif(replicate('[0-9]', len(substring(@ip, 1, charindex('.', @ip) - 1))), '') is not null And nullif(replicate('[0-9]', len(substring(@ip, charindex('.', @ip) + 1, charindex('.', @ip, charindex('.', @ip) + 1) - charindex('.', @ip) - 1))), '') is not null And nullif(replicate('[0-9]', len(reverse(substring(reverse(@ip), charindex('.', reverse(@ip)) + 1, charindex('.', reverse(@ip), charindex('.', reverse(@ip)) + 1) - charindex('.', reverse(@ip)) - 1)))), '') is not null And nullif(replicate('[0-9]', len(reverse(substring(reverse(@ip), 1, charindex('.', reverse(@ip)) - 1)))), '') is not null And isnumeric(replace(@ip, '.', '')) = 1 And convert(int, substring(@ip, 1, charindex('.', @ip) - 1)) <= 255 and convert(int, substring(@ip, charindex('.', @ip) + 1, charindex('.', @ip, charindex('.', @ip) + 1) - charindex('.', @ip) - 1)) <= 255 and convert(int, reverse(substring(reverse(@ip), charindex('.', reverse(@ip)) + 1, charindex('.', reverse(@ip), charindex('.', reverse(@ip)) + 1) - charindex('.', reverse(@ip)) - 1))) <= 255 and convert(int, reverse(substring(reverse(@ip), 1, charindex('.', reverse(@ip)) - 1))) <= 255 print 'y' else print 'n' |