SQL ASCII() Function: Introduction

0
4
SQL ASCII FUNCTION Example | ASCII Function In SQL

SQL ASCII() function is used to return the numeric value of a character given as an input to the function. The ASCII() function acts opposite to that of the CHAR function. ASCII stands for American Standard Code for Information Interchange.

SQL ASCII()

SQL ASCII is a built-in function that accepts a character expression and returns the ASCII code value of the leftmost character of the character expression.

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, 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, 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, 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, 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

 

If 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.

That’s it for this tutorial.

Recommended Posts

SQL Substring()

SQL CONCAT()

SQL Replace()

SQL String Functions

SQL Try Catch

Leave A Reply

Please enter your comment!
Please enter your name here

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