Ropardo Sowftware development company

Experience software development with ROPARDO S.R.L.

RSS Feed
RSS Feed
  • Home
  • About ROPARDO S.R.L
  • Our websites

Extending PostgreSQL – A better CONCAT operator

PostgreSQL users have the option of extending the set of functions and operators available. If you have a common SQL or programmatic routine, custom functions can be an effective way to more succinctly and efficiently accomplish your tasks. Likewise, custom operators can be created to call these functions (or existing built-in functions) in order to make more efficient and legible SQL statements. PostgreSQL provides four kinds of functions: query language functions (functions written in SQL), procedural language functions (functions written in, for example, PL/pgSQL or PL/Tcl), internal functions, C-language functions.

Creating custom functions or operators in PostgreSQL is not difficult at all. Here is an example of a user-defined operator that I find very useful.

A better CONCAT operator for PostgreSQL

In PostgreSQL, (unlike Oracle for example) concatenation with a null column (using || operator) nulls the result.
Here’s a quick example:

CREATE TABLE persons (
id        SERIAL      PRIMARY KEY,
firstname TEXT,
surname   TEXT        NOT NULL,
title     TEXT
);
 
INSERT INTO persons(firstname, surname, title)
VALUES ('Edward', 'Hyde', NULL);
INSERT INTO persons(firstname, surname, title)
VALUES ('Henry', 'Jekyll', 'Dr.');
INSERT INTO persons(firstname, surname, title)
VALUES (NULL, 'Fogle', NULL);
 
SELECT * FROM persons;
 
id         firstname          surname            title
--         ---------          --------           ----
1          Edward             Hyde               NULL
2          Henry              Jekyll             Dr.
3          NULL               Fogle              NULL
 
SELECT title || ' ' || firstname || ' ' || surname AS all_text FROM persons;
 
all_text
-----------------
NULL
Dr. Henry Jekyll
NULL

As you can see, the rows containing NULL values are omitted (concatenation result is NULL). This might not be the behavior you expect. There are several solutions to handle this situation, including the use of the SQL CASE WHEN expression, COALESCE or ARRAY_TO_STRING built-in functions.
But a more interesting and elegant approach is to make use of the PostgreSQL extensibility and to create an alternate version of the CONCAT operator.
First, we define a function which takes two TEXT values, and returns:
- One value if one of them is null (null if both are null)
- Concatenation of the values with a space between them if neither is null

CREATE OR REPLACE FUNCTION my_concat ( text, text )
RETURNS text AS
'SELECT
CASE WHEN $1 IS NULL THEN $2
WHEN $2 IS NULL THEN $1
ELSE $1 || '' '' || $2 END;'
LANGUAGE SQL IMMUTABLE;

Then we create a custom operator “||+”

CREATE OPERATOR ||+ (LEFTARG = TEXT,  RIGHTARG = TEXT, PROCEDURE = my_concat);

Using this new operator in a query is very easy and will produce a more natural result.

SELECT title ||+ firstname ||+ surname AS all_text FROM persons;
 
all_text
----------------------
Edward Hyde
Dr. Henry Jekyll
Fogle
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Get Shareaholic
Tags: CONCAT operator custom operators Extending PostgreSQL PostgreSQL

 Posted in: PostgreeSQL
May 4, 2010 | Alexandru Gyulai | 7 Comments

7 Responses

  • nursing schools
    June 21, 2010
    1

    Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

  • WP Themes
    June 22, 2010
    2

    Nice dispatch and this mail helped me alot in my college assignement. Thank you as your information.

  • federal student loan
    July 19, 2010
    3

    Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  • Rui Pinto
    July 21, 2010
    4

    Brilliant

  • aleksptiz
    July 31, 2010
    5

    I would like to exchange links with your site blog.ropardo.ro
    Is this possible?

  • jerseys
    September 16, 2010
    6

    One again, your idea is very

    good.thank you!very much.

  • David Frankson
    November 18, 2010
    7

    I was looking for a replacement for SQL Server’s + operator so it would treat NULL as ”, and could work with ints, floats, etc. I had to define 3 functions to over the operator. Posting here to save someone else time:

    CREATE OR REPLACE FUNCTION concat1(text,anyelement)
    RETURNS text AS
    $BODY$

    SELECT COALESCE($1,”) || COALESCE(CAST($2 as text),”)

    $BODY$
    LANGUAGE sql IMMUTABLE;

    CREATE OR REPLACE FUNCTION concat2(anyelement, text)
    RETURNS text AS
    $BODY$

    SELECT COALESCE($1,”) || COALESCE(CAST($2 as text),”)

    $BODY$
    LANGUAGE sql IMMUTABLE;

    CREATE OR REPLACE FUNCTION concat3(text, text)
    RETURNS text AS
    $BODY$

    SELECT COALESCE($1,”) || COALESCE($2,”)

    $BODY$
    LANGUAGE sql IMMUTABLE;

    CREATE OPERATOR + (LEFTARG = text, RIGHTARG = anyelement, PROCEDURE = concat);
    CREATE OPERATOR + (LEFTARG = anyelement, RIGHTARG = text, PROCEDURE = concat2);
    CREATE OPERATOR + (LEFTARG = text, RIGHTARG = text, PROCEDURE = concat3);

Leave a Reply

 


  • « Previous post
  • Next post »
  • Recent Posts

    • Installing PyGraphviz on Windows
    • Convert python object to XML representation
    • Liferay Portlet Development
    • Norway Road Show 2011 private meeting invitation
    • Oracle OpenWorld 2011
  • Ropardo is Hiring

  • Subscribe

    • Add to Google Reader or Homepage Add to netvibes TopOfBlogs
  • Recent Comments

    • Rajkumar Pomaji on Bluetooth PC Remote Control
    • Stelian Morariu on GWT 2.1 – Uploading a file using the RPC mechanism
    • Sergio on GWT 2.1 – Uploading a file using the RPC mechanism
    • Artem on Liferay: Deployment will start in a few seconds… and how to realy start
    • rkd80 on GWT 2.1 – Uploading a file using the RPC mechanism
  • Archives

    • November 2011 (1)
    • September 2011 (4)
    • July 2011 (3)
    • June 2011 (2)
    • May 2011 (4)
    • April 2011 (4)
    • March 2011 (3)
    • February 2011 (2)
    • January 2011 (2)
    • December 2010 (1)
    • November 2010 (4)
    • October 2010 (4)
    • August 2010 (3)
    • July 2010 (3)
    • June 2010 (6)
    • May 2010 (8)
    • April 2010 (7)
    • March 2010 (9)
    • February 2010 (6)
    • January 2010 (5)
    • December 2009 (7)
    • November 2009 (9)
    • October 2009 (10)
    • September 2009 (14)
    • August 2009 (10)
    • July 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (1)
    • October 2008 (3)
    • October 2007 (3)
    • July 2007 (4)
    • June 2007 (1)
    • May 2007 (3)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • News (15)
    • Ropardo Team (8)
    • Ropardo Products (6)
      • File Tracking Client (4)
      • iManagement (2)
    • Software Development (83)
      • Microsoft.NET (22)
      • Java (40)
      • Oracle (8)
      • Power Builder (3)
      • Liferay (5)
      • Lotus Notes (9)
      • xWiki (4)
    • System Adminstration (13)
      • Linux (10)
      • Windows (3)
    • Programming (1)
    • Uncategorized (3)
    • Databases (10)
      • MSSQL (5)
      • PostgreeSQL (3)
    • Microsoft.NET (1)
    • Web Development (28)
      • ASP/ASPX (3)
      • Content Management Systems (1)
      • HTML/CSS (5)
      • Javascrip/AJAX (8)
      • PHP (7)
    • Oracle E Business Suite (6)
  • Tags

    .NET ajax blog C# certification client CMS control css database Debugging django Domino Eclipse extension file tracking filter fun gentoo google Hibernate how to html image iManagement import Java javascript jQuery liferay Linux Lotus Notes lotus script Oracle Oracle BI Publisher 11g PHP portal PostgreSQL powerbuilder Python SQL Telerik velocity xml Xwiki

© 2010 ROPARDO s.r.l..

Powered by WordPress. Styled by Ropardo