CHAR() function in SQL Server
CHAR() :
This function helps to convert an int ASCII code to character value i.e if the user pass an integer as an argument, the CHAR() function interpret the integer value and returns its corresponding character value.
Syntax :
CHAR(integer_value)
Parameters :
This function accepts only one argument.
- integer_value –
Any integer value ranging from 0 to 255.
Returns :
- The function will return the character value that corresponds to the given integer code.
- The function will return NULL if the integer value exceeds the given range.
Applicable to following versions :
- SQL Server 2017
- SQL Server 2016
- SQL Server 2014
- SQL Server 2012
- SQL Server 2008 R2
- SQL Server 2008
- SQL Server 2005
Example-1 :
Basic usage of CHAR() function, the function will return ‘P’ and ‘D’.
SELECT CHAR(80) AS 'Character1', CHAR(100) AS 'Character2';
Output :
Character1 | Character2 |
---|---|
P | d |
Example-2 :
If the range of integer values is exceeded in CHAR() function, then the function will return NULL.
SELECT CHAR(260) AS 'Outcome';
Output :
Outcome |
---|
NULL |
Example-3 :
How to control Characters in CHAR() function, we use strings and CHAR() function between them,
SELECT 'Vansh' + CHAR(13) + 'vgaur12345@gmail.com' AS 'Name_Email';
Output :
Name_Email |
---|
Vansh vgaur12345@gmail.com |
Example-4 :
If the user passes two arguments in CHAR() function, the function will return an error.
SELECT CHAR(91, 200) AS 'Outcome';
Output :
The char function requires 1 argument(s)