- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - ASCII() Function
The SQL ASCII() function returns the decimal representation of the first character in a character string based on its code point in the ASCII character set.
The ASCII function takes one argument of any character data type. Returns an integer value, based on the first character of the argument.
If the argument is NULL or if the argument is an empty string, the ASCII function returns NULL.We can also use the ASCII function with string fields in an SQL table.
Note − For upper case alphabets(A to Z) the ASCII values = 65 to 90. For lower case alphabets(a to z) the ASCII values = 97 to 122.
Following is the syntax of the SQL ASCII() function −
SELECT ASCII (expr);
Parameters
- expr − It gives a string or character value.
Return value
It returns the ASCII value of a Character or left most character in a string.
Example
The following SELECT query displays the SQL ASCII value of the first character of the given string.
SELECT ASCII ("SQL stands for Structured Query Language") AS ASCII_S;
Output
Following is the output of the above query −
+---------+ | ASCII_S | +---------+ | 83 | +---------+
In the above data the string is "SQL stands for Structured Query Language", the first character is upper case âSâ and the ASCII value of that alphabet is 83. So the Query displayed the same value.
Example
The following SELECT query displays the ASCII value of the string we have given.
select ASCII ("Kaushik");
Output
The above SQL query produces the following output −
+-------------------+ | ASCII ("Kaushik") | +-------------------+ | 75 | +-------------------+
In above we know ASCII means the representation of the first character. So, even though if we are not the specifying character of the string. It will display the output of the string character as the ASCII values.
Example
The following SELECT query shows the ASCII value of the 'B' character.
SELECT ASCII("B");
Output
On executing the above query, it will produce the following output −
+------------+ | ASCII("B") | +------------+ | 66 | +------------+
In above Query, we have directly passed the character for the ASCII value.
Example
You can pass the table column as an argument to the ASCII() function to convert the character or string into a ASCII Value. Assume we have created a table with the name Customers using the CREATE statement as follows −
create table CUSTOMERS( ID INT NOT NULL, NAME VARCHAR(15) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(25), SALARY DECIMAL(10, 4), PRIMARY KEY(ID) );
Now let's insert seven records into the customers table using the INSERT statement as follows:−
insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(1, 'Ramesh', 32, 'Ahmedabad', 2000.00); insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(2, 'Khilan', 25, 'Delhi', 1500.00); insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(3, 'kaushik', 23, 'Kota', 2000.00); insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(4, 'Chaitali', 25, 'Mumbai', 6500.00); insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(5, 'Hardik', 27, 'Bhopal', 8500.00); insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(6, 'Komal', 22, 'MP', 4500.00); insert INTO CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY) VALUES(7, 'Muffy', 24, 'Indore', 10000.00);
The following Query shows the ASCII value of the first or leftmost character in the NAME column from the table. −
SELECT NAME, ASCII(NAME) As ASCIIvalue from CUSTOMERS;
Output
After executing the above statement, it produces the following output −
+----------+------------+ | NAME | ASCIIvalue | +----------+------------+ | Ramesh | 82 | | Khilan | 75 | | kaushik | 107 | | Chaitali | 67 | | Hardik | 72 | | Komal | 75 | | Muffy | 77 | +----------+------------+
Example
The following SELECT query uses the ASCII function with the NAME, and ADDRESS columns of those CUSTOMERS whose AGE is greater than 24 in the above CUSTOMERS table.
SELECT ID, NAME, ASCII(NAME), ADDRESS, ASCII(ADDRESS) FROM CUSTOMERS WHERE AGE > 24;
Output
After executing the above statement, it produces the following output −
+----+----------+-------------+-----------+----------------+ | ID | NAME | ASCII(NAME) | ADDRESS | ASCII(ADDRESS) | +----+----------+-------------+-----------+----------------+ | 1 | Ramesh | 82 | Ahmedabad | 65 | | 2 | Khilan | 75 | Delhi | 68 | | 4 | Chaitali | 67 | Mumbai | 77 | | 5 | Hardik | 72 | Bhopal | 66 | +----+----------+-------------+-----------+----------------+