ASCII Function in SQL

The ASCII function of Structured Query Language shows the ascii value of the first character of the string. We can also use the ASCII function with the string fields of the SQL table.

Syntax of ASCII String Function

Syntax1: This syntax uses the ASCII function with the column names of the SQL table:

In the syntax, we have to specify the column's name on which we want to use the ASCII string function.

Syntax2: This syntax uses the ASCII function with the set of characters (string):

Syntax2: This syntax uses the ASCII function with the individual character:

Examples of ASCII String function

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

Output:

ASCII_J
74

Example 3: The following SELECT query shows the ASCII value of the first character of the given string:

Output:

ASCII_N
78

Example 3: The following SELECT query shows the ASCII value of the 'R' character:

Output:

ASCII_R
82

Example 4: This example uses the ASCII function with the SQL table

In this example, we are going to create a new SQL table on which we have to execute the ASCII function for finding the ASCII value of first character.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Grade table:

The below INSERT queries insert the records of students with grades and marks in the Student_Grade table:

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


Roll_NoFirst_NameLast_NameFirst_CitySecond_CityNew_CityHindi_MarksMaths_MarksGrade
10AmanSharmaLucknowChandigarhGhaziabad8895A2
02VishalSharmaChandigarhGhaziabadGhaziabad9582A1
07RajGuptaDelhiGhaziabadLucknow9195A1
04YashSinghaniaGhaziabadDelhiLucknow8582A2
11VinayRoyDelhiKanpurGhaziabad9597A1
16ManojGuptaGhaziabadMeerutChandigarh9590B1
19RamGuptaLucknowGhaziabadChandigarh8995A2

Query 1: The following SELECT query uses the ASCII function with the Last_Name column of the above Student_Grade table:

This SQL statement shows the ASCII value of the first character of the last name from the table.

Output:

Last_NameASCII _LastName
SHARMA83
SHARMA83
GUPTA71
SINGHANIA83
ROY82
GUPTA71
GUPTA71

Query 2: The following SELECT query uses the ASCII function with the First_City, Second_City, and New_City columns of those students whose Roll_No is greater than 2 in the above Student_Grade table:

Output:

Roll_NoASCII(First_City)ASCII(Second_City)ASCII(New_City)
07687176
04716876
11687571
16717767
19767167