w3resource

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

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.

code:

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

Sample Output:

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

PostgreSQL UPPER() function using Column :

Sample Table: employees.


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:

code:

SELECT employee_id,first_name, 
ascii(first_name) "ASCII value of first character",
ascii(lower(first_name)) "ASCII value of first character" 
FROM employees 
WHERE department_id=100;

Sample 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)

Previous: UPPER function
Next: BTRIM function



Follow us on Facebook and Twitter for latest update.