Skip to content

Latest commit

 

History

History
112 lines (85 loc) · 3.69 KB

ascii-transact-sql.md

File metadata and controls

112 lines (85 loc) · 3.69 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
ASCII (Transact-SQL)
ASCII (Transact-SQL)
markingmyname
maghan
11/14/2019
sql
t-sql
reference
ASCII_TSQL
ASCII
ASCII function
characters [SQL Server], ASCII
code [SQL Server], ASCII
leftmost character of expression
TSQL
>= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric

ASCII (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

Returns the ASCII code value of the leftmost character of a character expression.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

ASCII ( character_expression )  

Arguments

character_expression
An expression of type char or varchar.

Return types

int

Remarks

ASCII stands for American Standard Code for Information Interchange. It serves as a character encoding standard for modern computers. See the Printable characters section of ASCII for a list of ASCII characters.

ASCII is a 7-bit character set. Extended ASCII or High ASCII is an 8-bit character set that is not handled by the ASCII function.

Examples

A. This example assumes an ASCII character set, and returns the ASCII value for 6 characters.

SELECT ASCII('A') AS A, ASCII('B') AS B,   
ASCII('a') AS a, ASCII('b') AS b,  
ASCII(1) AS [1], ASCII(2) AS [2];  

[!INCLUDEssResult]

A           B           a           b           1           2  
----------- ----------- ----------- ----------- ----------- -----------  
65          66          97          98          49          50  

B. This examples shows how a 7-bit ASCII value is returned correctly, but an 8-bit Extended ASCII value is not handled.

SELECT ASCII('P') AS [ASCII], ASCII('æ') AS [Extended_ASCII];

[!INCLUDEssResult]

ASCII       Extended_ASCII
----------- --------------
80          195

To verify if the results above map to the correct character code point, use the output values with the CHAR or NCHAR function:

SELECT NCHAR(80) AS [CHARACTER], NCHAR(195) AS [CHARACTER];

[!INCLUDEssResult]

CHARACTER CHARACTER
--------- ---------
P         Ã

From the previous result, notice that the character for code point 195 is à and not æ. This is because the ASCII function is capable of reading the first 7-bit stream, but not the extra bit. The correct code point for character æ can be found using the UNICODE function, which is capable or returning the correct character code point:

SELECT UNICODE('æ') AS [Extended_ASCII], NCHAR(230) AS [CHARACTER];

[!INCLUDEssResult]

Extended_ASCII CHARACTER
-------------- ---------
230            æ

See also

CHAR (Transact-SQL)
NCHAR (Transact-SQL)
UNICODE (Transact-SQL)
String Functions (Transact-SQL)