ASCII (Transact-SQL)

返回字符表达式中最左侧的字符的 ASCII 代码值。

主题链接图标Transact-SQL 语法约定

语法

ASCII ( character_expression )

参数

  • character_expression
    char 或 varchar 类型的表达式

返回类型

int

示例

以下示例针对 Du monde entier 字符串中的每个字符假定一个 ASCII 字符集并返回 ASCII 值以及 CHAR 字符。

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

下面是结果集:

----------- - 
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