AppDividend
Latest Code Tutorials

SQL ASCII FUNCTION Example | ASCII Function In SQL

0

SQL ASCII FUNCTION Example | ASCII Function In SQL is today’s topic. ASCII stands for American Standard Code for Information Interchange. SQL ASCII function is used to return the numeric value of a character which is given as an input to the function. This function just acts opposite to that of CHAR function.

SQL ASCII FUNCTION Example

The ASCII function accepts a character expression and returns the ASCII code value of the leftmost character of the character expression. See the following syntax.

Syntax

SELECT ASCII (single_character or string);

PARAMETERS

  1. Single_character: It is a specified character whose numeric value will be returned.
  2. String: If a sequence of characters is inserted in function as input then only the first character, the numeric value will be returned ignoring all the remaining characters.

Example

See the following query.

Select Ascii (‘A’);

See the following output.

65

EXPLANATION

As the ASCII value of A is 65, so it has been returned as output.

See the following query.

Select Ascii (‘a’);

See the output.

97

EXPLANATION

As the ASCII value of a is 97, so it has been returned as output.

See the following third query.

Select ASCII (‘Appdividend.com’);

See the output.

65

EXPLANATION

As the above input was a string so only the first character was returned ignoring all the characters.

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
201 Shiva Rana Jalandhar Punjab 20000
301 Karan Kumar Allahabad Uttar Pradesh 40000
401 Suraj Bhakat Kolkata West Bengal 60000
501 Akash Cherukuri Vizag Andhra Pradesh 70000

 

Suppose we want to print the Numeric Code for the first character of Emp_Name, then the following query has to be considered.

QUERY

See the following query.

Select Emp_name, ASCII(Emp_name) AS NumCode from Employee;

See the output.

Emp_name NumCode
Rohit Raj 82
Shiva Rana 83
Karan Kumar 75
Suraj Bhakat 83
Akash Cherukuri 65

So, you can see from the output that the Numeric value of the first character is returned under the column name NumCode.

Finally, SQL ASCII FUNCTION Example | ASCII Function In SQL is over.

Recommended Posts

SQL Substring Function Example

SQL CONCAT Function Example

SQL Replace Function Example

SQL String Functions Example

Sql Try Catch 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.