Find a certain char in a string


Userlevel 4

Goal  

Sometimes there is a weird character in a string, could be in a data exchange for example. The goal of this procedure is to find which ascii code this sign is so you can easily replace it. It makes it easier to find if the sign is a tab or maybe a return. In the example you will see that a return exists of 2 chars, the 13 and the 10.

solution

declare @tab table (ascii_value int
,char_value nvarchar(1))

-- create variables for the character string and for the current position in the string.
declare @position int = 1
,@string nvarchar(100) ;

set @string = '§
'

while @position <= len(@string)
begin
insert into @tab
select ascii(substring(@string, @position, 1))
,char(ascii(substring(@string, @position, 1)))

set @position = @position + 1
end

select * from @tab

 

Result

ascii_value char_value
167 §
13  
10  

 


0 replies

Be the first to reply!

Reply