PostgreSQL CHR() function
CHR() function
The PostgreSQL chr function is used to return the corresponding character against the given code within the argument. This function is useful for converting numeric ASCII codes back to their character representations, which can be helpful in various text processing and data manipulation tasks.
Uses of CHR() Function
- Character Conversion: Convert numeric ASCII codes to their corresponding characters.
- Data Formatting: Generate special characters based on their ASCII codes for formatting purposes.
- Custom Character Generation: Create characters dynamically in queries or applications.
- String Construction: Build strings with specific characters based on their ASCII values.
- Text Data Manipulation: Enhance text processing by converting codes to characters as needed.
Syntax:
chr(number)
PostgreSQL Version: 9.3
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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/PostgreSQL/chr-function.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics