MS SQL Server CHAR() Function

Sometimes you may need to convert an integer ASCII value to a character in MS SQL Server. There is a function called CHAR in MS SQL Server by which you can do this. CHAR function converts an integer ASCII value to a character. To know more about ASCII characters you can see the Printable ASCII Characters section of my another post ASCII Characters.

Let’s see the function syntax:

CHAR(integer_expression);

In this syntax:

  • In this function there is one parameters of integer_expression.
  • Parameter integer_expressionis is an integer from 0 through 255.
  • Parameter integer_expressioncan be a constant, variable, or column.
  • Returns CHAR returns a NULL value for integer expressions outside this range, or when then integer expresses only the first byte of a double-byte character.
  • Function returns a character of an integer expression.

The following example uses CHAR function to convert an integer ASCII value to a character.

SELECT CHAR(65) AS A, CHAR(90) AS Z,   
CHAR(97) AS a, CHAR(122) AS z,  
CHAR(48) AS [0], CHAR(57) AS [9]; 

Here is the result set.

A Z a z [0] [9]
A Z a z 0 9

I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.

In this tutorial, I have shown you how to convert an integer ASCII value to a character in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.

Add a Comment