ASCII (Transact-SQL)

 

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

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

Topic link icon Transact-SQL Syntax Conventions

-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse  
  
ASCII ( character_expression )  

character_expression
Is an expression of the type char or varchar.

int

The following example assumes an ASCII character set and returns the ASCII value and CHAR character for each character in the string Du monde entier.

SET TEXTSIZE 0;  
SET NOCOUNT ON;  
-- Create the variables for the current character string position   
-- and for the character string.  
DECLARE @position int, @string char(15);  
-- Initialize the variables.  
SET @position = 1;  
SET @string = 'Du monde entier';  
WHILE @position <= DATALENGTH(@string)  
   BEGIN  
   SELECT ASCII(SUBSTRING(@string, @position, 1)),  
      CHAR(ASCII(SUBSTRING(@string, @position, 1)))  
    SET @position = @position + 1  
   END;  
SET NOCOUNT OFF;  
GO  

Here is the result set.

----------- -   
68          D   
  
----------- -   
117         u   
  
----------- -   
32              
  
----------- -   
109         m   
  
----------- -   
111         o   
  
----------- -   
110         n   
  
----------- -   
100         d   
  
----------- -   
101         e   
  
----------- -   
32              
  
----------- -   
101         e   
  
----------- -   
110         n   
  
----------- -   
116         t   
  
----------- -   
105         i   
  
----------- -   
101         e   
  
----------- -   
114         r  

The following 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];  

Here is the result set.

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

String Functions (Transact-SQL)

Community Additions

ADD
Show: