print ''

declare @ip varchar(15)
set @ip = '2.1.232.22' -- '23..232'
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
                        cast(substring(@ip, 1, charindex('.', @ip) - 1) as int) <= 255 and
                        cast(substring(@ip, charindex('.', @ip) + 1,
                                charindex('.', @ip, charindex('.', @ip) + 1) -
                                        charindex('.', @ip) - 1) as int) <= 255 and
                        cast(reverse(substring(reverse(@ip), charindex('.', reverse(@ip)) + 1,
                                charindex('.', reverse(@ip), charindex('.', reverse(@ip)) + 1) -
                                        charindex('.', reverse(@ip)) - 1)) as int) <= 255 and
                        cast(reverse(substring(reverse(@ip), 1,
                                charindex('.', reverse(@ip)) - 1)) as int) <= 255
print 'y' 
else 
print 'n'

This page was last updated on May 01, 2006 04:28 PM.