Nulls

Started by Michael Davisalmost 27 years ago1 messages
#1Michael Davis
michael.davis@prevuenet.com

I have a function:

void *nz(void *bp) {
text *new_text;
new_text = (text *) palloc(sizeof(text)+2);
memset((void *) new_text, 0, sizeof(text)+2);
strcpy(VARDATA(new_text), ":");
VARSIZE(new_text) = sizeof(text)+2;
return (void *)(new_text);
}

I define the function in psql:

CREATE FUNCTION nz(text) RETURNS text AS
'/usr/src/pgsql/contrib/spi/psql_ext.so' LANGUAGE 'c';

I call the function:

select addressid, nz(state) from addresses where addressid = 3870;

If state is NULL, nz(state) still returns NULL! How can I get my nz()
function to reflect something other than NULL? The purpose behind this is
to have a concatenated string return a value even if any value is null. For
example:

select addressid, nz(state) || nz(country) from addresses where addressid =
3870;

Returns NULL if state is null or if country is null. I want it to return
the country if state is null.