CHAR() Function in SQL Server

Rumman Ansari   Software Engineer   2023-03-25   5702 Share
☰ Table of Contents

Table of Content:


Syntax:


CHAR(Integer_Expression) 

Converts an int ASCII code to a character. The Integer_Expression, should be between 0 and 255. The following SQL, prints all the characters for the ASCII values from o thru 255

Code:


Declare @Number int
Set @Number = 1
While(@Number <= 255)
Begin
 Print CHAR(@Number)
 Set @Number = @Number + 1
End

Note: The while loop will become an infinite loop, if you forget to include the following line.


Set @Number = @Number + 1

Code: Printing uppercase alphabets using CHAR() function:



Declare @Number int
Set @Number = 65
While(@Number <= 90)
Begin
 Print CHAR(@Number)
 Set @Number = @Number + 1
End

Code: Printing lowercase alphabets using CHAR() function:



Declare @Number int
Set @Number = 97
While(@Number <= 122)
Begin
 Print CHAR(@Number)
 Set @Number = @Number + 1
End

Code: Another way of printing lower case alphabets using CHAR() and LOWER() functions.



Declare @Number int
Set @Number = 65
While(@Number <= 90)
Begin
 Print LOWER(CHAR(@Number))
 Set @Number = @Number + 1
End