Sunday, February 17, 2013

SQL SERVER CHAR String FUNCTION


SQL SERVER CHAR String FUNCTION

CHAR function accepts ascii value of the character and returns character value
for ex:   select char(65) ,char(ascii('America')) -->  A  A     65 is ASCII value of capital 'A'.
              select char(97) ,char(ascii('america')) -->  a  a     97 is ASCII value of small 'a'.

Suppose u want to displayCHAR values along with  ASCII values of all characters in the string.
Here is the Procedure.
declare @position int,@ttx nchar(25)
set @position=1;
set @ttx='America!';
set nocount off;
while @position <= LEN(@ttx)
begin
select ascii(substring(@ttx,@position,1)) as ASCII,CHAR(ascii(substring(@ttx,@position,1))) as CHAR;
set @position = @position + 1;
end
O/P:
ASCII    CHAR
65    A
ASCII    CHAR
109    m
ASCII    CHAR
101    e
ASCII    CHAR
114    r
ASCII    CHAR
105    i
ASCII    CHAR
99    c
ASCII    CHAR
97    a
ASCII    CHAR
33    !
tags:SQL Server ASCII String function,SQL Server ASCII function,return ASCII value of String in SQL-Server

No comments:

Post a Comment