Re: ask: select right(column) ???

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: ask: select right(column) ???
Date: 2009-02-16 11:44:55
Message-ID: 20090216114455.GT32672@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Feb 16, 2009 at 03:21:20PM +0700, hendra kusuma wrote:
> select right(column, number_of_character) from table
[..]
> honestly I have no idea that such simple feature doesn't exist in postgresql
> or am I wrong? since I look at SQL Key Words table and it's written as
> reserved

AFAIK, it's reserved because "right" is used in outer join syntax; i.e.
table RIGHT OUTER JOIN table, and not because of the function name.

If you want this in Postgres, you could always do:

CREATE FUNCTION right(TEXT,INTEGER) RETURNS TEXT
LANGUAGE sql IMMUTABLE STRICT AS $$
SELECT substring($1 FROM char_length($1)-$2+1) $$;

Using "right" to extract the right most characters from a string sounds
very much like BASIC to me but may have moved to other languages now.
Humm, lets have a look; BASIC has Right$(), Pascal has RightStr(), MySql
has Right(), MS Sql Server has Right(). The other languages I chose to
look at (C, C++, Java, PHP, Lua, Ruby, Javascript) all naively expose a
substring function, like PG and Oracle, and leave anything like Right()
up to the user.

--
Sam http://samason.me.uk/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Serge Rielau 2009-02-16 14:13:04 Re: Which SQL is the best for servers?
Previous Message Sam Mason 2009-02-16 11:09:59 Re: left outer join without rows from "left" table