SQL CHAR | SQL Server CHAR() Function With Example
Summary: in this tutorial, you will learn SQL CHAR Function and how to use the CHAR() function in SQL Server to convert an ASCII code value to a character. This is most popular string function in SQL.
Definition and Usage
The CHAR() function returns the character which is based on the ASCII code. The CHAR() function converts an ASCII value to a character value. Furthermore, let see the syntax of the CHAR() function in SQL Server.
SQL CHAR() Function Synatx
There are the following synatx:
Parameter Values
Parameter | Description |
---|---|
int_exp | Required. in this syntax, the int_expr is an integer expression, which evaluates to an integer whose value from 0 to 255. Therefore, if the integer expression evaluates to a value that is outside of this range, then the CHAR() function returns NULL. |
To convert a character to an ASCII value, then you use the ASCII() function
SQL CHAR() Function Example:
Therefore, the following as given below example uses the CHAR() function to get the characters of the number 65, 72, 89, and 90.
1 2 3 4 5 |
SELECT CHAR(65) char_65, CHAR(72) char_72, CHAR(89) char_89, CHAR(90) char_90; |
Therefore, let’s execute the query to return the output.
NOTE 1:
Furthermore, Let’s see the following statement will returns the NULL because the argument is out of the range from 0 through 255
1 2 |
SELECT CHAR(1001) out_of_range; |
The following shows the output:
1 2 3 4 |
out_of_range ------------ NULL (1 row affected) |
NOTE 2:
Therefore, the following given below example uses the CHAR function in SQL Server to concat two strings. However, the 2nd (second) string is placed in the new line:
1 |
SELECT 'Welcome,' + CHAR(10) + 'London' AS Result; |
Let’s execute the query to return the output.
1 2 3 4 5 |
Result ------------ Welcome, London (1 row affected) |
Therefore, In the above example, the CHAR 10 will returns the new line character, furthermore, the second string is placed in the new line.
Conclusion
In this tutorial, you have learned CHAR() Function and also how to use the CHAR() function in SQL Server to get a corresponding character from an ASCII value.