PostgreSQL String() Function: Display the first word form a particular column

11. Write a query to display the first word in the job title if the job title contains more than one words.

Sample Solution:


SELECT job_title,  SUBSTR(job_title,1, POSITION(' ' IN job_title)) 
FROM jobs;

Sample table: jobs


pg_exercises=# SELECT job_title,  SUBSTR(job_title,1, POSITION(' ' IN job_title))
pg_exercises-# FROM jobs;
            job_title            |     substr
 President                       |
 Administration Vice President   | Administration
 Administration Assistant        | Administration
 Finance Manager                 | Finance
 Accountant                      |
 Accounting Manager              | Accounting
 Public Accountant               | Public
 Sales Manager                   | Sales
 Sales Representative            | Sales
 Purchasing Manager              | Purchasing
 Purchasing Clerk                | Purchasing
 Stock Manager                   | Stock
 Stock Clerk                     | Stock
 Shipping Clerk                  | Shipping
 Programmer                      |
 Marketing Manager               | Marketing
 Marketing Representative        | Marketing
 Human Resources Representative  | Human
 Public Relations Representative | Public
(19 rows)

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

What is the difficulty level of this exercise?

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