PostgreSQL/Postgre SQL/String Calculation

Материал из SQL эксперт
Перейти к: навигация, поиск

Assigning a concatenated value to a string

postgres=#
postgres=#
postgres=# -- Assigning a concatenated value to a string
postgres=#
postgres=# CREATE FUNCTION title_and_author (text, text) RETURNS text AS "
postgres"#     DECLARE
postgres"#         -- Declare aliases for the two function arguments.
postgres"#        title ALIAS for $1;
postgres"#        author ALIAS for $2;
postgres"#        result text;
postgres"#
postgres"#     BEGIN
postgres"#        result := title || "", by "" || author;
postgres"#         -- Return the resulting string.
postgres"#        return result;
postgres"#
postgres"#     END;
postgres"# " language "plpgsql";
CREATE FUNCTION
postgres=#
postgres=# select title_and_author("a","b");
 title_and_author
------------------
 a, by b
(1 row)
postgres=#
postgres=#



Joined words

postgres=# CREATE FUNCTION "compound_word" (text,text) RETURNS text AS "
postgres"#      DECLARE
postgres"#        -- defines an alias name for the two input values
postgres"#        word1 ALIAS FOR $1;
postgres"#        word2 ALIAS FOR $2;
postgres"#      BEGIN
postgres"#        -- displays the resulting joined words
postgres"#        RETURN word1 || word2;
postgres"#      END;
postgres"#   " LANGUAGE "plpgsql";
CREATE FUNCTION
postgres=#
postgres=# select compound_word("www.sqle.ru"," Postgre SQL");
       compound_word
----------------------------
 www.sqle.ru Postgre SQL
(1 row)
postgres=#
postgres=#