SQL - Loop through a string and show all the ASCII characters

 

declare @pos int
declare @field varchar(1000)
select @field = remarks from DynColumn where TABLE_NAME = 'pa01001' 
        and COLUMN_NAME = 'patu'
set @pos = 0
declare @char varchar(1)
while @pos < LEN(@field)
begin
    set @char = SUBSTRING(@field,@pos,1)
      
    print convert(char(10),@pos) + 
        case 
            when ascii(@char) in (10) then 'LF   ' 
            when ascii(@char) in (13) then 'CR   ' 
            when ascii(@char) in (9) then 'TAB  ' 
            else @char + '    ' end 
        convert(char(10),ascii(@char))
    set @pos = @pos + 1 
  
end
        

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences