What is the difference between ASCII() and CHAR() functions?

ASCII() vs CHAR():

ASCII() and CHAR() functions work with ASCII characters. But the functionality wise, both work quiet opposite.

ASCII() function returns the ASCII code number for the given character as input. Where as, CHAR() function returns the ASCII character for the given ASCII code number.

ASCII():

Input: Character/String

Output: Code number

Syntax: ASCII(character_expression)

Note: This function returns the code number of the first character if we pass the string as an input.

Examples:

————————————————————————
SELECT ASCII(‘S’) AS AscForS;
— Result: 83 –> 83 is the ASCII value for the character ‘S’
SELECT ASCII(‘SQLVersity’) AS AscForString;
— Result: 83 –> Retruns ASCII value for the ‘S’
— as it considers only the first character of the string.
SELECT ASCII(‘123’) AS AscForNumbers;
— Result: 49 –> returns ASCII value of 1.
SELECT ASCII(‘!@#$’) AS AscForSymbols;
— Result: 33 –> Returns ASCII value of !.
SELECT ASCII(‘&$)VG234S’) AS AscForAlphNum;
— Result: 38 –> Returns ASCII value of &.
————————————————————————-

CHAR():

Input:  Code number

Output: Character

Syntax: CHAR(number_code)

Examples:

——————————–
SELECT CHAR(65);
— Result: ‘A’
SELECT CHAR(35);
— Result: ‘#’
SELECT CHAR(45645);
— Result: NULL
SELECT CHAR(56);
— Result: ‘8’
SELECT CHAR(150);
— Result: ‘-‘
——————————–

Note: It returns NULL if the character does not exist for the code number which we have passed.

ASCII codes table:

ASCII

Happy Learning  :-) 

Tags:,

Leave a Reply

Your email address will not be published. Required fields are marked *