Skip to content

LEFT

Return the first n characters in a string. If n is a negative number, return all but the last n characters. (See also RIGHT.)

LEFT(string, number)

For example, the following query uses the LEFT and RIGHT functions to extract goals scored from the ftscore column and find out which matches featured 10 or more goals:

premdb=# SELECT * FROM match 
WHERE LEFT(ftscore,1)::INT+RIGHT(ftscore,1)::INT >=10 
ORDER BY seasonid, matchday;
 seasonid |      matchday       | htid | atid | ftscore | htscore 
----------+---------------------+------+------+---------+---------
      16 | 2007-09-29 00:00:00 |   32 |   83 | 7-4     | 2-1
      16 | 2007-12-29 00:00:00 |   42 |   83 | 6-4     | 1-1
      18 | 2009-11-22 00:00:00 |   42 |   95 | 9-1     | 1-0
      20 | 2011-08-28 00:00:00 |   26 |   51 | 8-2     | 3-1
      21 | 2012-12-29 00:00:00 |    2 |   77 | 7-3     | 1-1
      21 | 2013-05-19 00:00:00 |   44 |   75 | 5-5     | 1-3
(6 rows)

Note that the sum of the LEFT and RIGHT results is only possible because these values are cast from character strings to integers first.

Parent topic:String Functions