All,

Getting the first 4 characters from the begining of a string is easy enough:

  SELECT SUBSTR('ABCDEFGHIJKLMNOP', 1, 4);

Returns 'ABCD'. But getting the last 4 characters appears to be a little more work and is ugly:

  SELECT SUBSTR('ABCDEFGHIJKLMNOP', LENGTH('ABCDEFGHIJKLMNOP') - 3, 4);

Returns 'MNOP'. I hate having to provide my input string more than once like this. So ... uglier:

  REGEXP_REPLACE('ABCDEFGHIJKLMNOP', '^.*(....)$', '\\1');

Returns 'MNOP'. Many languages have a version of substr that takes negative arguments to begin offset from the end of the string like this:

  SELECT SUBSTR('ABCDEFGHIJKLMNOP', -4);

That doesn't seem to work in PostgreSQL. In fact, it doesn't even error out ... it just returns the whole string. Is there an easy (preferred) method that I'm missing?

-- Dante

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
      choose an index scan if your joining column's datatypes do not
      match

Reply via email to