CHAR function is used to convert an integer value to it's equivalent character.
It is also used to insert control characters into strings. Below table specifies control character and it's value.
Control Character | Value |
Tab | CHAR(9) |
Line Feed | CHAR(10) |
Carriage return | CHAR(13) |
Syntax of CHAR Function :
CHAR ( integer_expression )
integer_expression is an integer from 0 through 255. If integer_expression is not in specified range than it returns NULL.
Return type of CHAR function is char type.
Examples of CHAR Function :
Example 1 : Use of CHAR function in select clause
SELECT CHAR(83)
Output
S
Above example displays the use of CHAR function to display character based on ascii value.
Example 2 : Use of CHAR function in where clause and nesting string functions
SELECT ContactName, CompanyName
FROM Customers
WHERE CHAR(ASCII(ContactName)) = CHAR(ASCII(CompanyName))
Output
ContactName CompanyName
Ana Trujillo Ana Trujillo Emparedados y helados
Antonio Moreno Antonio Moreno Taquería
Above example displays all customers, where first character of contact name and company name is same. It also shows how can we nest string functions.