CHAR (Transact-SQL)

Converte un codice ASCII di tipo int in un carattere.

Icona di collegamento a un argomentoConvenzioni della sintassi Transact-SQL

Sintassi

CHAR ( integer_expression )

Argomenti

  • integer_expression
    Valore intero compreso tra 0 e 255. Viene restituito Null viene restituito se l'espressione intera non è compresa in questo intervallo.

Tipi restituiti

char(1)

Osservazioni

È possibile utilizzare la funzione CHAR per inserire caratteri di controllo nelle stringhe di caratteri. Nella tabella seguente vengono descritti i caratteri di controllo più comunemente utilizzati.

Carattere di controllo

Valore

Scheda

char(9)

Avanzamento riga

char(10)

Ritorno a capo

char(13)

Esempi

A. Utilizzo di ASCII e CHAR per stampare valori ASCII da una stringa

In questo esempio viene stampato il valore e il carattere ASCII di ogni carattere della stringa New Moon.

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

Set di risultati:

----------- -

78 N

----------- -

101 e

----------- -

119 w

----------- -

32

----------- -

77 M

----------- -

111 o

----------- -

111 o

----------- -

110 n

----------- -

B. Utilizzo della funzione CHAR per inserire un carattere di controllo

Nell'esempio seguente viene utilizzato CHAR(13) per stampare nome e indirizzo di posta elettronica di un dipendente su righe diverse quando i risultati vengono restituiti in formato testo.

USE AdventureWorks2008R2;
GO
SELECT p.FirstName + ' ' + p.LastName, + CHAR(13)  + pe.EmailAddress 
FROM Person.Person p JOIN Person.EmailAddress pe
ON p.BusinessEntityID = pe.BusinessEntityID
AND p.BusinessEntityID = 1;
GO

Set di risultati:

Ken Sanchez

ken0@adventure-works.com

(Righe interessate: 1)