By: Greg Robidoux
The CHAR function is used to get the actual character when the ASCII value is provided.
Syntax
CHAR(value)
Simple CHAR Example
SELECT CHAR(49) SELECT CHAR(50) SELECT CHAR(51) SELECT CHAR(65) SELECT CHAR(66) SELECT CHAR(67)
We get the following values:
1 2 3 A B C
Get List of All CHAR Values and Integer Value
The following will get a list of the CHAR value and the corresponding character.
DECLARE @counter INT = 0 CREATE TABLE #CharValues ([char] nchar(1), [value] int) WHILE (@counter <= 255 ) BEGIN BEGIN TRY INSERT INTO #CharValues SELECT CHAR(@counter), @counter SET @counter = @counter + 1 END TRY BEGIN CATCH; SET @counter = @counter + 1 IF @counter > 255 BEGIN BREAK END END CATCH END SELECT [value], [char] FROM #CharValues DROP TABLE #CharValues
Here are a few rows from the query results.
Related Articles
- SQL Server Char Function and Reference Guide - this includes a table of values
- SQL ASCII
Last Update: 11/2/2021