Home » CHAR Function in SQL

CHAR Function in SQL

by

CHAR Function in SQL

The CHAR string function shows the ASCII value of the integer passed in the function. This function takes only one argument. If we pass the integer value which exceeds the given range then it shows NULL value.

Syntax of CHAR String Function

In SQL, we can use the CHAR function with the columns of the table, strings, and characters.

Syntax 1:

In this syntax, we used the CHAR function with existing table of SQL. Here, we have to define the name and integer column of that table on which we want to perform CHAR function.

Syntax 2:

In this syntax, we used the CHAR function with the integer value.

Examples of CHAR String function

Example 1: The following SELECT query shows the ASCII value of 20:

Output:

ASCII_of_20
^T

Example 2: The following SELECT query shows the ASCII value of 84:

Output:

ASCII_of_84
T

Example 3: The following SELECT query shows the ASCII value of 260:

Output:

ASCII_of_98
NULL

Example 4: This example uses the CHAR function with the table in Structured Query Language.

In this fourth example, we will create the new table through which we will perform the CHAR function with table values:

The following block shows the syntax to create the new table in SQL:

The following CREATE statement creates the Fresher_Marks table:

The below INSERT queries insert the records of freshers with marks and details in the Fresher_Marks table:

The following SELECT statement displays the inserted records of the above Fresher_Marks table:

Fresher_IDFirst_NameMiddle_NameLast_NameCityAptitude_MarksReasoning_MarksTechnical_MarksPercentage
501VinayRoyGuptaLucknow85927885
92MonuRoySinghaniaChandigarh54689888
104RaviRoyKumarLucknow71826971
7ShyamRoySharmaDelhi85906878
85AbhayKumarGuptaChandigarh45688278
509RiyaRoySharmaDelhi68906991
65VishalKumarSharmaMumbai75658875

Query 1: The following SELECT query uses the CHAR function with the Fresher_ID column of the above Fresher_Marks table:

This SQL statement shows the ASCII value of id of each fresher from the above table:

Output:

Fresher_IDFirst_NameASCII_of_Fresher_ID
501VinayNULL
92Monu
104Ravih
7Shyam^G
85AbhayU
509RiyaNULL
65VishalA

Query 2: The following SELECT query uses the CHAR function with the Aptitude_Marks column of the above Fresher_Marks table:

This SQL statement shows the ASCII value of aptitude marks of each fresher from the above table:

Output:

Fresher_IDAptitude_MarksASCII_of_Aptitude_Marks
50185U
92546 (Digit)
10471G
785U
8545
50968D
6575K

Query 3: The following SELECT query uses the CHAR function with the Reasoning_Marks and Technical_Marks column of the above Fresher_Marks table:

This SQL statement shows the ASCII value of reasoning and technical marks of each fresher from the above table:

Output:

Fresher_IDReasoning_MarksASCII_of_Reasoning_MarksTechnical_MarksASCII_of_Technical_Marks
501926 (Digit)78N
9268D98b
10482R69E
790Z68D
8568D82R
50990Z69E
6565A88X

Query 4: The following SELECT query uses the CHAR function with the Percentage column of the above Fresher_Marks table:

Output:

Fresher_IDPercentageASCII_of_Percentage
50185U
9288X
10471G
778N
8578N
50991[
6575K

You may also like