SQL - CHAR() Function



The SQL CHAR() function can be used to retrieve the American Standard Code for Information Interchange (ASCII) character representing a given integer value. The value returned by this function is of the string datatype. This function has only one argument. If we pass an integer value that exceeds the given range, a NULL value will be displayed.

The CHAR() function does not support multiple integer arguments, if we try to pass multiple values it generates an error saying "The char function requires 1 argument(s)"

We can also use this function with along with the table columns, by passing them as parameters, along with strings, and characters.

Syntax

Following is the syntax of the SQL CHAR() function −

SELECT CHAR(Integer_Value);

Example

The following SELECT query displays the CHAR value of 100 −

SELECT CHAR(100) AS char_function;

Output

Following is the output of the above query −

+---------------+
| char_function |
+---------------+
|      d        |
+---------------+

Example

Following is an example which uses the SQL CHAR() function to get the characters of the number 72 and 97 −

SELECT CHAR(72) char_72, CHAR(97) char_97;

Output

Following is the output of the above query −

+---------+----------+
| char_72 |  char_97 |
+---------+----------+
|   H     |    a     |
+---------+----------+

Example

Following is an example where the range of the ASCII values is from 0 to 255. So here if we are passing the argument as out of range, returns the null value −

SELECT CHAR(300) out_of_range;

Output

On executing the above query, it will produce the following output −

+--------------+
| out_of_range |
+--------------+
|   NULL       |
+--------------+

Example

Following is an example uses the CHAR() function to concat two strings, then the second string is placed in the new line −

SELECT 'Hello,' + CHAR(10) + 'There' AS Char_function;

Output

On executing the above query, it will produce the following output −

+--------------+
| Char_function|
+--------------+
| Welcome,     |
|   Back       |
+--------------+

Example

In the following example we are passing an empty string as a parameter to the CHAR() function returns empty −

SELECT CHAR('');

Output

On executing the above query, it will produce the following output −

+------------+
| CHAR('')   |
+------------+
|            |
+------------+

Example

In the following example we are passing a NULL value as a parameter to the CHAR() function returns null −

SELECT CHAR(NULL);

Output

On executing the above query, it will produce the following output −

+------------+
| CHAR(NULL) |
+------------+
|   NULL     |
+------------+

Example

You can also pass the table column as an argument to the SQL CHAR(). Assume we have created a table with the name STUDENTS using the CREATE statement as follows −

CREATE TABLE STUDENTS(
   ID INT NOT NULL, 
   NAME VARCHAR(15) NOT NULL, 
   MARKS INT NOT NULL, 
   GRADE INT, PRIMARY KEY(ID)
);

Now let's insert seven records into the customers table using the INSERT statement as follows:−

INSERT INTO STUDENTS VALUES(1, 'Ramesh', 90, 65);
INSERT INTO STUDENTS VALUES(2, 'Khilan', 80, 66);
INSERT INTO STUDENTS VALUES(3, 'kaushik', 30, 67);
INSERT INTO STUDENTS VALUES(4, 'Chaitali', 25, 68);

The following SELECT query uses the NCHAR() function with the ID column of the above CUSTOMERS table −

SELECT ID, NAME, CHAR(GRADE) AS GRADE FROM STUDENTS;
 

Output

Following is the output of the above query −

+----+---------+----------+
| ID |   NAME  |  GRADE   |
+----+---------+----------+
| 1  | Ramesh  |     A    |
| 2  | Khilan  |     B    |
| 3  | kaushik |     C    |
| 4  | Chaitali|     D    |
+----+---------+----------+
sql-string-functions.htm
Advertisements