AppDividend
Latest Code Tutorials

SQL Char Function Example | Char Function In SQL Tutorial

0

SQL Char Function Example | Char Function In SQL Tutorial is today’s topic. SQL CHAR function is used for converting a numeric value to a character. It is just the opposite of the ASCII function. A character or string function is a function which takes one or more characters or numbers as parameters and returns the character value.

SQL Char Function

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

See the following syntax.

Select CHAR (number_code)

Number_code

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 ASCII value of a is 97 so character a is printed when number 97 was given as an input to the function.

Let’s see the second query.

SELECT CHAR (65);

See the output.

A

Explanation

As 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

Let’s apply the ASCII function to a table.

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 support 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 is in the contrast to MySQL’s CHAR() function which allows you to provide multiple integers in the argument.

Out of Range Integers

The function also doesn’t support 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 the MySQL’s CHAR() function, which accepts the integers larger than 255 in which case, they’re automatically 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’s what 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 |
+--------+-----------------------+

Finally, Char Function In SQL Tutorial is over.

Recommended Posts

ASCII Function In SQL

Stuff Function In SQL Tutorial

trim() Function In SQL

SQL Substring Function Example

SQL CONCAT Function Example

SQL Replace Function Example

SQL Check Constraint Example

SQL String Functions Example

SQL Try Catch Example

SQL NULL Functions Example

Leave A Reply

Your email address will not be published.

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