AppDividend
Latest Code Tutorials

SQL Char() Function: The Complete Guide

SQL CHAR() function returns the character based on the ASCII code.

SQL CHAR

SQL CHAR() is an inbuilt function used to convert a numeric value to a character. It is just the opposite of the ASCII() function. A character or string function is a function that takes one or more characters or numbers as parameters and returns the character value.

Syntax

See the following syntax.

Select CHAR (number_code)

Parameters

The number from which character is to be retrieved. An integer outside the range 0 to 255 will return a NULL character.

See the following code.

SELECT CHAR (97);

See the output.

a

Explanation

As the ASCII value of a is 97, character a is printed when number 97 is given input to the function.

See the second query.

SELECT CHAR (65);

See the output.

A

Explanation

As the ASCII value of A is 65, so character A is printed when number 65 was given as an input to the function.

Range of ASCII values for characters

A-Z: 65-90
a-z: 97-122

Table: Employee

Emp_id Emp_name City State Salary
101 Rohit Raj Patna Bihar 30000
102 Shiva Rana Jalandhar Punjab 20000
103 Karan Kumar Allahabad Uttar Pradesh 40000
104 Suraj Bhakat Kolkata West Bengal 60000
105 Akash Cherukuri Vizag Andhra Pradesh 70000

 

Suppose we want to print the Character Code for Emp_id of Emp_Name, then the following query has to be considered.

See the following query.

Select Emp_name, CHAR(Emp_id) AS CharCode 
from Employee;

See the output.

Emp_name CharCode
Rohit Raj e
Shiva Rana f
Karan Kumar g
Suraj Bhakat h
Akash Cherukuri i

 

So, you can see from the output that the Character Code of Employee Id is returned under the column name CharCode.

SQL CHAR function can also be used as control characters.

CONTROL CHARACTER VALUE
Tab CHAR(9)
Line Feed CHAR(10)
Carriage return CHAR(13)

Multiple Integers

The char() function doesn’t use the multiple integers as arguments.

If you provide multiple integers, you’ll get an error.

See the following code example.

SELECT CHAR(67, 255) AS 'Result';

See the output.

The char function requires 1 argument(s).

Note that this contrasts with MySQL’s MySQL’sfunction, which allows you to provide multiple integers in the argument.

Out of Range Integers

The function also doesn’t the integers outside a range of 1 to 255. If your argument is outside that range, the result is NULL.

See the following query.

SELECT CHAR(256) AS 'Result';

See the following output.

+----------+
| Result   |
|----------|
| NULL     |
+----------+

This is again in contrast to MySQL’s MySQL’sfunction, which accepts the integers larger than 255 in which case, they’re converted into multiple result bytes.

Inserting Control Characters

See the following code.

SELECT 'Homer' + CHAR(13) + 'krunal@appdividend.com' AS 'Name/Email';

See the output.

+--------------+
| Name/Email   |
|--------------|
| Homer
krunal@appdividend.com              |
+--------------+

Here it looks like if we remove the CHAR(13):

SELECT 'KRUNAL' AS 'Name', 'krunal@appdividend.com' AS 'Email';

See the output.

+--------+-----------------------+
| Name   | Email                 |
|--------+-----------------------|
| KRUNAL |krunal@appdividend.com |
+--------+-----------------------+

That’s iThat’sthis tutorial.

Recommended Posts

ASCII Function In SQL

Stuff Function In SQL

trim() Function In SQL

SQL Substring Function

SQL CONCAT Function

SQL Replace Function

SQL Check Constraint

SQL String Functions

SQL Try Catch

SQL NULL Functions

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.