pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Davis <michael.davis@tvguide.com>
To: 'pgsql-sql@postgreSQL.org' <pgsql-sql@postgreSQL.org>
Subject: RE: [SQL] Trouble with null text fields
Date: Tue, 13 Apr 1999 11:01:34 -0500
Message-ID: <93C04F1F5173D211A27900105AA8FCFC14539B@lambic.prevuenet.com> (raw)

Try this:
DROP FUNCTION nz(text);
CREATE FUNCTION nz (text) RETURNS text AS '
		BEGIN
		if ($1 is NULL) then
return """;
		else 
		return $1; end if;
	END; ' LANGUAGE 'plpgsql';
SELECT (nz(field_one) || nz(field_two)) from t1;
This has worked for me.  It is a little slow. 
	-----Original Message-----
	From:	Glenn Waldron [SMTP:gwaldron@wareonearth.com]
<mailto:[SMTP:gwaldron@wareonearth.com]> 
	Sent:	Tuesday, April 13, 1999 9:26 AM
	To:	pgsql-sql@postgreSQL.org <mailto:pgsql-sql@postgreSQL.org> 
	Subject:	[SQL] Trouble with null text fields

Using Postgres 6.5 beta (snap 12 apr), on Linux i386.  I moved up
from
6.4.2 when I couldn't get things working.
I'm having difficulty dealing with null text/varchar fields.  I need to be
able to interpret null values as the null string " for the purposes on
concatenation.

1)	----
	For example, the query:
	SELECT (field_one || field_two) from t1;
Will return the concatenation of the two fields.  If either of the
fields
is null, it is interpreted as the empty string " and the correct
answer
is printed.  But:
	SELECT * from t1 where ( field_one || field_two = 'something' )
This does NOT work is either field_one or field_two is null.  Same
result
with the textcat() function.

2)	----
	Next I tried using "case", getting a parse error at or near "then":
SELECT ( case field_one when null then " else field_one end ) from t1;
This one gave me "ERROR: copyObject: don't know how to copy 704":
SELECT ( case field_one when 'string' then 'other' else 'third' end)
from t1;


3)	---
I tried writing a function that takes a "text" type and returns "
is the
string
is null.  Never could successfully do a null test on a function
parameter.

4)	----
I also tried writing my own concat function, and found that passing
null fields into a user function doesn't seem to work either.  So I
tried
passing the whole thing in as a TUPLE, since you can determine
whether a
field is null with the GetAttributeByName() call.
The creation:
		CREATE FUNCTION mycat(text,text) returns text as
'/usr/.../file.so'
langauge 'sql';
This worked great, even with null values:
	SELECT mycat(field_one, field_two) from t1;
This crashed the backend, with a "Memory exhauted in AllocSetAlloc()" error:
SELECT * from t1 where mycat(field_one, field_two) = 'something';
So I tried making an index, and got:  "DefineIndex(): Attibute t1
not found"
	CREATE INDEX t1_ix on t1 ( mycat(t1) text_ops );

Any help is appreciated!! Sorry for the novel!!  -glenn
	



view thread (6+ messages)  latest in thread

Message-ID: <93C04F1F5173D211A27900105AA8FCFC14539B@lambic.prevuenet.com>
Permalink:  ../93C04F1F5173D211A27900105AA8FCFC14539B@lambic.prevuenet.com/
Also on:    postgresql.org/message-id/93C04F1F5173D211A27900105AA8FCFC14539B@lambic.prevuenet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: michael.davis@tvguide.com
  Subject: Re: [SQL] Trouble with null text fields
  In-Reply-To: <93C04F1F5173D211A27900105AA8FCFC14539B@lambic.prevuenet.com>
  References: <93C04F1F5173D211A27900105AA8FCFC14539B@lambic.prevuenet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox