SQL Server ASCII() Functions: Returns ASCII Code

In SQL Server, the ASCII() function returns the ASCII code value of the leftmost character of the specified number or character value.

ASCII stands for American Standard Code for Information Interchange. It serves as the character encoding standard for electronic communication.

ASCII(string)

Parameters

string: A string or char value.

Return Value

Returns the ASCII code for the specified character or string. If more than one character is entered, then the function returns the ASCII code for the first (leftmost) character.

Example 1:

The following example demonstrates the ASCII() function and returns an ASCII code for a character and string. For string value, it returns the ASCII code of the first character.

Example: ASCII()
SELECT ASCII('A') A, ASCII('ABC') ABC

Example 2:

You can pass the column of a database table as a parameter. In the following example, the ASCII() function returns the ASCII code for FirstName and LastName column values.

Example: ASCII()
SELECT FirstName as EmpName, ASCII(FirstName) as ASCIIFirstValue,
LastName as EmpLastName, ASCII(LastName) as ASCIILastName
From Employee;

Example 3:

The following example returns ASCII code for numbers and special characters.

Example: ASCII()
SELECT ASCII('9') AS [9], ASCII('&') AS [&], ASCII(99) AS [99], ASCII('/') AS [/]

ASCII function can be used to remove unwanted special characters from a string. If written as a function in SQL Server, characters from ASCII range 128 to 255 (Extended ASCII characters) will be removed. For example, the function can be used on the string 'Õ192ÖxyÕ67' to return 192xy67.

ASCII function can be used to generate passwords, exclude numbers or characters from columns or print alphabets or special characters.

Note: Printable ASCII characters can be formed into text-based visual art.

Want to check how much you know SQL Server?