Dec 24, 2014

SQL Server: ASCII & CHAR String Functions in SQL Server

Ascii: This function Returns the ASCII code value as integer of the leftmost character of a character expression.
Syntax:
Ascii( character_expression )
Return Type:
Int
CHAR – Fixed-length non-Unicode character data with length of n bytes
Syntax:
Char(integer_expression)
Return Type
Char(1)
Example:- Below example is explaining how to change a character to integer value using Ascii function and same as integer to char value.

SELECT Ascii('a') AS ASCI, CHAR(Ascii('a')) AS [CHAR] UNION
SELECT Ascii('b') AS ASCI, CHAR(Ascii('b')) AS [CHAR] UNION
SELECT Ascii('c') AS ASCI, CHAR(Ascii('c')) AS [CHAR] UNION
SELECT Ascii('d') AS ASCI, CHAR(Ascii('d')) AS [CHAR] UNION
SELECT Ascii('e') AS ASCI, CHAR(Ascii('e')) AS [CHAR]

Result:
ASCI   CHAR
97     a
98     b
99     c
100    d
101    e
SELECT Ascii('A') AS ASCI, CHAR(Ascii('A')) AS [CHAR] UNION
SELECT Ascii('B') AS ASCI, CHAR(Ascii('B')) AS [CHAR] UNION
SELECT Ascii('C') AS ASCI, CHAR(Ascii('C')) AS [CHAR] UNION
SELECT Ascii('D') AS ASCI, CHAR(Ascii('D')) AS [CHAR] UNION
SELECT Ascii('E') AS ASCI, CHAR(Ascii('E')) AS [CHAR]

Result:
ASCI   CHAR
65     A
66     B
67     C
68     D
69     E