SQL Server CHAR() Function
The SQL Server (Transact-SQL) CHAR() function returns the single-byte character with the specified integer_code, as defined by the character set and encoding of the default collation of the current database.
The range of integer_code is from 0 through 255. The function returns a NULL value for integer_code outside this range or not representing a complete character. The function also returns a NULL value when the character exceeds the length of the return type. Many common character sets share ASCII as a sub-set and will return the same character for integer values in the range 0 through 127.
Syntax
CHAR(integer_code)
Parameters
integer_code |
Required. Specify integer whose character value (according to the ASCII table) is to be retrieved. |
Return Value
Returns the single-byte character given by the code value of the specified integer_code.
Example 1:
The example below shows the usage of CHAR() function.
SELECT CHAR(72); Result: 'H' SELECT CHAR(69); Result: 'E' SELECT CHAR(NULL); Result: NULL SELECT CHAR('73'); Result: 'I'
Example 2:
Consider a database table called Sample with the following records:
Data | x1 | x2 | x3 |
---|---|---|---|
Data1 | 67 | 117 | 116 |
Data2 | 80 | 117 | 116 |
Data3 | 84 | 111 | 111 |
Data4 | 66 | 111 | 119 |
Data5 | 67 | 79 | 68 |
Data6 | 69 | 110 | 100 |
The statement given below can be used to get the string containing characters given by the code values specified by columns x1, x2 and x3.
SELECT *, CHAR(x1) + CHAR(x2) + CHAR(x3) AS CHAR_String FROM Sample;
The query will produce the following result:
Data | x1 | x2 | x3 | CHAR_String |
---|---|---|---|---|
Data1 | 67 | 117 | 116 | Cut |
Data2 | 80 | 117 | 116 | Put |
Data3 | 84 | 111 | 111 | Too |
Data4 | 66 | 111 | 119 | Bow |
Data5 | 67 | 79 | 68 | COD |
Data6 | 69 | 110 | 100 | End |
❮ SQL Server Functions