views:

1687

answers:

2

In MSSQL, I can do

SELECT ISNULL(Field,'Empty') from Table

But in PGSQL i get a syntax error. how do I emulate the ISNULL functionality ?

+12  A: 
SELECT case field WHEN field3 IS NULL THEN 'Empty' END as fieldAlias

Or more idiomatic:

SELECT coalesce(field3, 'Empty') as fieldAlias
Kyle Butt
+1 for `coalesce`. (P.S. You can do that in MS SQL Server, too.)
Alison R.
There are other cases for using IS NULL though, so it's good to know both.
Kyle Butt
I think it's worth noting that it is `coalesce` that is in SQL standard, with `isnull` being an MS-specific function that essentially is `coalesce` with only two parameters.
GSerg
`COALESCE` is ANSI standard, and supported SQL Server 2000+
OMG Ponies
Coalesce() also handles type promotion properly (exactly like UNION SELECT does), while IsNull() does not.
Emtucifor
+1  A: 

Create the following function

CREATE OR REPLACE FUNCTION isnull(text, text) RETURNS text AS 'SELECT (CASE (SELECT $1 "
    "is null) WHEN true THEN $2 ELSE $1 END) AS RESULT' LANGUAGE 'sql'

And it'll work.

You may to create different versions with different parameter types.