PostgreSQL ASCII Function

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PostgreSQL ASCII() function

ASCII() function

The PostgreSQL ASCII function is used to get the code of the first character of a given string.

Syntax :

ascii(<string>)

PostgreSQL Version : 9.3

Pictorial Presentation of PostgreSQL ASCII() function

Example : PostgreSQL ASCII() function :

In the following example, PostgreSQL ascii function returns the code of the first character of the given string.

  1. SELECT ascii('w3resource'AS "ASCII of first character";  

Output :

 ASCII of first character
--------------------------
                      119
(1 row)

PostgreSQL UPPER() function using Column :

If we want to display the employee_id, first name, ASCII value of first letter of first_name and ASCII value of the 1st letter of first_name after convert it in lower case for those employees who belong to the department which department_id is 100 from employees table , the following SQL can be executed:

  1. SELECT employee_id,first_name,   
  2. ascii(first_name) "ASCII value of first character",  
  3. ascii(lower(first_name)) "ASCII value of first character"   
  4. FROM employees   
  5. WHERE department_id=100;  

Output :

 employee_id | first_name  | ASCII value of first character | ASCII value of first character
-------------+-------------+--------------------------------+--------------------------------
         108 | Nancy       |                             78 |                            110
         109 | Daniel      |                             68 |                            100
         110 | John        |                             74 |                            106
         111 | Ismael      |                             73 |                            105
         112 | Jose Manuel |                             74 |                            106
         113 | Luis        |                             76 |                            108
(6 rows)