This section is from the "Practical PostgreSQL" book, by John Worsley and Joshua Drake. Also available from Amazon: Practical PostgreSQL.
Concatenating means to combine two strings together.
This simple example joins the first and second string of words supplied to become a compound word.
Example 9-15. Concatenating Text
CREATE FUNCTION compound_word(text, text) RETURNS text AS'
DECLARE
-- defines an alias name for the two input values
word1 ALIAS FOR $1;
word2 ALIAS FOR $2;
BEGIN
-- displays the resulting joined words
RETURN word1 || word2;
END;
' LANGUAGE 'plpgsql';
When the words break and fast are passed as arguments to the compound_word() function, it displays:
SELECT compound_word('break', 'fast');
compound_word
---------------
breakfast
(1 row)
![]() | Dealing with Text in PostgreSQL |
|---|---|
When the words break and fast are supplied to the function, they are inside of single quotes because the function takes a text argument. You will recieve an error if any text string is passed without the single quotes. |
 
Continue to: