How to Use ASCII() Function in Postgres

PostgreSQL provides a wide range of string functions that are utilized to retrieve some information regarding a specific string. ASCII() function is one of those functions. This function gives the ASCII code of a character. The character whose ASCII code is to be found is passed into the function as a parameter.

This article will contain a detailed demonstration of the working of the ASCII() function in PostgreSQL.

How to Use the ASCII() Function in PostgreSQL?

As discussed in the introduction, the ASCII() function returns the ASCII code for the character that is passed into it. The syntax of the ASCII() function is given as:

ASCII(Character);

In the above syntax:

● The character whose ASCII number is to be found is passed into the ASCII() function.

● The function will return an integer as the ASCII code of the provided character.

There are some important points regarding the ASCII() function:

● We can also find the ASCII code of the special characters using this ASCII() function.

● The ASCII code of upper and lower case letters is different.

● We can also provide a string to the ASCII() function. In this case, the function will return the ASCII code for only the first character of that string.

We will later understand all these cases with the help of examples. First, let’s see how the ASCII() function is used in Postgres.

Example 1: Using ASCII() Function

Let’s find the ASCII code for the letter “S” using the ASCII() function. The query for this can be written as:

SELECT ASCII('S') AS ASCII_code_capital_S;

The output of this query gives the ASCII code for the “S”, which is “83”:

img

Example 2: Using ASCII() Function with Special Characters

We can also find the ASCII code for the special characters in Postgres by utilizing the ASCII() function like this:

SELECT 
ASCII('&') AS ASCII_code;

The query will give the ASCII code for the “&” operator as:

img

The ASCII code of the “&” is 38.

Example 3: Using ASCII() Function With Upper and Lower Case Letters

We will write the following query to notice the ASCII code for the upper and lower case letters:

SELECT 
  ASCII('S') AS ASCII_code_capital_S,
  ASCII('s') AS ASCII_code_small_s;

The output of this query will be the ASCII code for the capital and small “s”. We will note that the code will be different for both. The output is:

img

Note that the ASCII code for the upper case “S” is 83 and for the lower case, it is 115. So the code is different when changing the case of the character.

Let’s implement the ASCII() function on a string.

Example 4: Using ASCII() Function With String

We can also provide a string of more than one character in the ASCII() function. But the function will give the ASCII code of only the first character/letter of that provided string. Let’s write up the following query to observe the output:

SELECT 
  ASCII('S') AS ASCII_code_S,
  ASCII('Smith') AS ASCII_code_Smith;

The above query will give the ASCII code for the letter “S” and the string “Smith” as:

img

We can see that the ASCII code for the string “Smith” is the same as that of the letter “S”. So this point is verified that the ASCII() function gives the ASCII code for the first character/letter of the provided string.

This is how the ASCII() function works to find the ASCII code of the given character/string in PostgreSQL.

Conclusion

The ASCII() function is used to find the ASCII code for the given characters. This character can be a special character as well. If we pass a string into the ASCII() function, the function will give us the ASCII code of only the first character of that string, not the whole string. The ASCII code is case sensitive, which means that the lower and upper case of the same letter have different ASCII codes. In this post, we have discovered the workings of the ASCII() function along with its use cases.