ASCII() Function in SQL Server
The ASCII() function returns the ASCII value of the leftmost character of a character expression.
Syntax :
ASCII(character_expression)
Parameter :
This method accepts a single-parameter as mentioned above and described below :
character_expression :
It can be a literal character, an expression of a string, or a column. If more than one character is entered, it will only return the value for the leftmost character.
Returns :
It returns the ASCII code value of its leftmost character.
Example-1 :
When the arguments hold the single uppercase and lowercase letter.
SELECT ASCII('A') AS A, ASCII('a') AS a, ASCII('Z') AS Z, ASCII('z') AS z;
Output :
A | a | Z | z |
---|---|---|---|
65 | 97 | 90 | 122 |
Example-2 :
When the arguments hold the single number and special character.
SELECT ASCII('1') AS [1], ASCII('#') AS #, ASCII(9) AS [9], ASCII('@') AS [@];
Output :
1 | # | 9 | @ |
---|---|---|---|
49 | 35 | 57 | 64 |
Example-3 :
When the arguments hold the expression of a string.
SELECT ASCII('w3wiki');
Output :
71
Example-4 :
Using ASCII() function with table columns.
Table – Player_Details
PlayerId | PlayerName | City |
---|---|---|
45 | Rohit Sharma | Mumbai |
18 | Virat Kohli | Bangalore |
7 | MS Dhoni | Chennai |
33 | Hardik Pandya | Mumbai |
42 | Sikhar Dhawan | Delhi |
SELECT PlayerName, ASCII(PlayerName) AS AsciiCodeOfFirstChar FROM Player_Details;
Output :
PlayerName | AsciiCodeOfFirstChar |
---|---|
Rohit Sharma | 82 |
Virat Kohli | 86 |
MS Dhoni | 77 |
Hardik Pandya | 72 |
Sikhar Dhawan | 83 |