MS SQL Server ASCII Function
Sometimes you may need to convert character data to ASCII code value in MS SQL Server. There is a function called ASCII
in MS SQL Server by which you can do this. ASCII
function returns the ASCII code value of the leftmost character of a character expression. To know more about ASCII characters you can see the Printable ASCII Characters section of my another post ASCII Characters.
Let’s see the function syntax:
ASCII(character_expression);
In this syntax:
- In this function there is one parameters of character_expression.
- Parameter character_expression is an expression of char or varchar type data.
- Parameter character_expression can be a constant, variable, or column.
- Returns data type of varchar or nvarchar data.
- Function returns the ASCII code value of the leftmost character of a character expression.
The following example uses ASCII
function to convert character data to ASCII code value.
SELECT ASCII('Ar-Razaak') AS A, ASCII('Z') AS Z, ASCII('a') AS a, ASCII('z') AS z, ASCII(0) AS [0], ASCII(9) AS [9];
Here is the result set.
A | Z | a | z | [0] | [9] |
---|---|---|---|---|---|
65 | 90 | 97 | 122 | 48 | 57 |
I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.
In this tutorial, I have shown you how to convert character data to ASCII code value in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.