SQL Server CHAR() Function: Returns ASCII of Integer

The CHAR() function returns the ASCII code of the specified integer from 0 to 255. You can also specify an expression that returns an integer value.




integer_expression: An integer from 0 to 255 or an expression that returns a value from 0 to 255.

Return Value

Returns a character with CHAR(1) data type. It returns a NULL if specified integers outside 0 to 255 range.

Control Character Value
Tab CHAR(9)
Line feed CHAR(10)
Carriage return CHAR(13)

Example 1:

The CHAR() function can be used with the SELECT statement to get the ASCII code of the specified value.

Example: CHAR()
select char(65) as char65 

Example 2:

Use CHAR() function in SELECT statements to insert special characters into strings. In the example below, FirstName and LastName are returned using different special characters. The first output column uses tab CHAR(9), the second output column uses CHAR(12), the third column uses CHAR(45) and the last column uses CHAR(35) and CHAR(33) ASCII values to separate the first and last name.

Example: CHAR()
select FirstName + CHAR(9) + LastName as EmpName,
FirstName + char(12) + LastName as EmpName,     
FirstName + char(45) + LastName as EmpName,
FirstName + char(35) + LastName + char(33) as EmpName
FROM Employee;

Example 3:

Use CHAR() function to insert control characters. In the example below, CHAR(13) (Carriage return) is used in the SELECT statement to print employee name and email in separate lines.

Example: CHAR()
select emp.FirstName + ' ' + emp.LastName + CHAR(13) + emp.email 
FROM Employee emp
WHERE EmployeeID = 2;

Example 4:

You can specify an expression that returns an integer value, as shown below.

Example: CHAR()
SELECT CHAR(10*10*2+30) as expression_ascii

Example 5:

In the following example, CHAR() function returns NULL when the specified integer is not within 0 to 255 range.

Example: CHAR()
SELECT CHAR(256) as OutOfRange;
Want to check how much you know SQL Server?