w3resource

PostgreSQL CHR() function

CHR() function

The PostgreSQL chr function is used to return the corresponding character against the given code within the argument.

Syntax:

chr(number)

PostgreSQL Version: 9.3

Pictorial Presentation of PostgreSQL CHR() function

Pictorial presentation of PostgreSQL CHR() function

Example: PostgreSQL CHR() function:

In the example below, the chr function returns the character against the corresponding code.

Code:

SELECT chr(90) AS "Character of CODE(90)";

Sample Output:

 Character of CODE(90)
-----------------------
 Z
(1 row)

Example of PostgreSQL CHR() function using column:

Sample Table: employees


Here in the example, we want to make a query without using LEFT(), SUBSTRING() function. We want to find those employees who start their first_name with the letter 'A'. Here we have displayed three columns i.e. empoloyee_id, first_name and last_name.

Code:

SELECT employee_id,first_name,last_name 
FROM employees
WHERE chr(ascii(first_name))='A';

Sample Output:

 employee_id | first_name | last_name
-------------+------------+-----------
         103 | Alexander  | Hunold
         115 | Alexander  | Khoo
         121 | Adam       | Fripp
         147 | Alberto    | Errazuriz
         158 | Allan      | McEwen
         167 | Amit       | Banda
         175 | Alyssa     | Hutton
         185 | Alexis     | Bull
         187 | Anthony    | Cabrio
         196 | Alana      | Walsh
(10 rows)

Previous: BTRIM function
Next: CONCAT function



Follow us on Facebook and Twitter for latest update.