将 int ASCII 代码转换为字符的字符串函数。
CHAR ( integer_expression )
integer_expression
介于 0 和 255 之间的整数。如果整数表达式不在此范围内,将返回 NULL 值。
char(1)
CHAR 可用于将控制字符插入字符串中。下表显示了一些常用的控制字符。
控制字符 | 值 |
---|---|
制表符 | CHAR(9) |
换行符 | CHAR(10) |
回车 | CHAR(13) |
下面的示例将打印字符串"New Moon"中每个字符的 ASCII 值和字符。
SET TEXTSIZE 0
-- Create variables for the character string and for the current
-- position in the string.
DECLARE @position int, @string char(8)
-- Initialize the current position and the string variables.
SET @position = 1
SET @string = 'New Moon'
WHILE @position <= DATALENGTH(@string)
BEGIN
SELECT ASCII(SUBSTRING(@string, @position, 1)),
CHAR(ASCII(SUBSTRING(@string, @position, 1)))
SET @position = @position + 1
END
GO
下面是结果集:
----------- -
78 N
----------- -
101 e
----------- -
119 w
----------- -
32
----------- -
77 M
----------- -
111 o
----------- -
111 o
----------- -
110 n
----------- -
下例使用 CHAR(13) 在不同的行上打印名称、地址与城市信息,并以文本方式返回结果。
USE Northwind
SELECT FirstName + ' ' + LastName, + CHAR(13) + Address,
+ CHAR(13) + City, + Region
FROM Employees
WHERE EmployeeID = 1
下面是结果集:
Nancy Davolio
507 - 20th Ave. E.
Apt. 2A
Seattle WA
说明 在此记录中,Address 列中的数据也包含一个控制字符。
相关文章