w3resource

PostgreSQL String() Function: Display specific columns for all employees and form the salary column to be left-padded with the dollar


14. Write a query to display the first name and salary for all employees. Form the salary to be 10 characters long, left-padded with the $ symbol. Label the column as SALARY.

Sample Solution:

Code:

SELECT first_name,
LPAD(TRIM(TO_CHAR(salary,'9999999')), 7, '$') SALARY 
FROM employees;

Sample table: employees


Output:

pg_exercises=# SELECT first_name,
pg_exercises-# LPAD(TRIM(TO_CHAR(salary,'9999999')), 7, '$') SALARY
pg_exercises-# FROM employees;
 first_name  | salary
-------------+---------
 Alexander   | $$$9030
 Bruce       | $$$6030
 David       | $$$4830
 Valli       | $$$4830
 Diana       | $$$4230
 Den         | $$11030
 Alexander   | $$$3130
 Shelli      | $$$2930
 Sigal       | $$$2830
 Steven      | $$24030
 Neena       | $$17030
 Guy         | $$$2630
 Karen       | $$$2530
 Jason       | $$$3330
 Michael     | $$$2930
 Ki          | $$$2430
 Hazel       | $$$2230
 Stephen     | $$$3230
 John        | $$$2730
 Joshua      | $$$2530
 Trenna      | $$$3530
...
 Susan       | $$$6530
 Hermann     | $$10030
 Shelley     | $$12030
 William     | $$$8330
(106 rows)

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query that displays the first name and the character length of the first name for all employees whose name starts with the letters 'A', 'J' or 'M'. Give each column an appropriate label. Sort the results by the employees' first names.
Next: PostgreSQL JOINS - Exercises, Practice, Solution

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming