Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need list of character that not satisfy like condition for example,
--Validation for X
declare @test varchar(50)
set @test='ds[fds'
if @test like '%[^0-9a-zA-Z .()/-]%'
print 'invalid -yes'
else
print 'invalid -no'

In above case i need output '[' which is not satisfy condition also if there is more than one invalid character then need that all

SQL
declare @phone varchar(50)
set @phone='212@@f'

select  REPLACE(@phone
               , SUBSTRING(@phone, PATINDEX('%[^0-9a-zA-Z .()/-]%', @phone), 1)
              , '')


but it does not work if there is sql character like '%' in input string so what is the solution.
SQL
declare @phone varchar(50)
set @phone='212%@@f'

select  REPLACE(@phone
               , SUBSTRING(@phone, PATINDEX('%[^0-9a-zA-Z .()/-]%', @phone), 1)
              , '')
Posted
Updated 20-Mar-14 17:45pm
v3

1 solution

SQL
declare @phone varchar(50)
set @phone='212%@@f'

declare @illegal varchar(50)
declare @test varchar(50)

set @illegal = SUBSTRING(@phone, PATINDEX('%[^0-9a-zA-Z .()/-]%', @phone), 1)
set @test = REPLACE(@phone, SUBSTRING(@phone, PATINDEX('%[^0-9a-zA-Z .()/-]%', @phone), 1), '')

while @test <> @phone
    begin
    set @phone = @test
    set @illegal = @illegal + SUBSTRING(@phone, PATINDEX('%[^0-9a-zA-Z .()/-]%', @phone), 1)
    set @test = REPLACE(@phone, SUBSTRING(@phone, PATINDEX('%[^0-9a-zA-Z .()/-]%', @phone), 1), '')
    end

select @illegal
select @phone
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900