1

I have a function like this:

FUNCTION addUser(name string, surname string) RETURNS text AS

and now, what I need is to take only 3 starting characteters of each argument and make a new string out of them and return it.

Can you point me in the right direction on how to do that?

Nikola
  • 14,888
  • 21
  • 101
  • 165

2 Answers2

4

left is awesome but only exists in >9.1. If you are stuck, like I am, on 8.3 or 8.4 you can use substr

CREATE FUNCTION addUser(IN in_name TEXT, IN in_surname TEXT) RETURNS TEXT AS
$$
SELECT substr($1,1,3) || substr($2,1,3);
$$
LANGUAGE SQL STABLE;
Lukas Eklund
  • 6,068
  • 1
  • 32
  • 33
  • No use casting `$1::text`, when `$1` id defined as `text` to begin with. And do not quote the language name! It's an identifier, not a value. [Can lead to very unpleasant surprises](http://stackoverflow.com/questions/12514664/postgresql-procedural-language-c-not-found/12530268). – Erwin Brandstetter Mar 30 '13 at 09:16
2
 return left(name,3) || left(surname,3);

More details about string functions are in the manual:

http://www.postgresql.org/docs/current/static/functions-string.html