將 int ASCII 代碼轉(zhuǎn)換為字符的字符串函數(shù)。
CHAR ( integer_expression )
integer_expression
介于 0 和 255 之間的整數(shù)。如果整數(shù)表達式不在此范圍內(nèi),將返回 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 列中的數(shù)據(jù)也包含一個控制字符。
相關文章