Search code examples
sqlpostgresqlstring-function

RIGHT() function in Postgres is not right


I'm reviewing the Postgres RIGHT() function documentation, right..

For the documentation example:

SELECT RIGHT('XYZ', 2);

I was expecting the right response as per the documentation viz;

 right
-------
 YZ
(1 row)

But then I ran the query in Aginity Workbench and the result wasn't right. I get:

found "RIGHT" (at char 8) expecting an identifier found a keyword

Can someone set me right?


Solution

  • The right function is only available beginning with Postgres version 9.1. I am guessing that the version used which generated that error message is using version 9.0 or earlier.

    See the documentation for 9.0 which does not have a right function, then see the documentation for 9.1 which shows that right is available.

    As a workaround, you may use substr:

    select
        substr('Hello World', char_length('Hello World') - 4, 5);
    
    World