- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
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 | +----+---------+----------+