473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

coalesce and nvl question

Hi

Is there a standard postgres method of replacing empty strings.

In Oracle, nvl handles nulls and empty strings, as does ifnull() in
MySQL, but with postgres coalesce only handles null strings.

If, not is the best solution to create a plpgsql function, ie

CREATE FUNCTION isEmpty (character varying, character varying) RETURNS
character varying
AS '
declare
fnRetTrue alias for $1;
fnRetFalse alias for $2;
begin
if fnRetTrue = '' or fnRetTrue is not null then
return fnRetTrue;
else
return fnRetFalse;
end if;
end;
'
LANGUAGE plpgsql;

Thanks

Simon

--
Simon Windsor
Email: si***********@cornfield.org.uk
Tel: 01454 617689
Mob: 07960 321599
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Mailscanner thanks transtec Computers for their support.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #1
5 14115
Simon Windsor wrote:
Hi

Is there a standard postgres method of replacing empty strings.

In Oracle, nvl handles nulls and empty strings, as does ifnull() in
MySQL, but with postgres coalesce only handles null strings.

If, not is the best solution to create a plpgsql function, ie

CREATE FUNCTION isEmpty (character varying, character varying) RETURNS
character varying


This all depends upon what you mean by handle. Do you want to treat
empty strings as NULL or NULL as empty strings? As you said, you can
treat NULL as empty strings using COALESCE:

SELECT COALESCE(x, '');

You can treat empty strings as NULL

SELECT NULLIF(x, '');

But I'd guess most on this list are wondering why you want to equate
an empty string with NULL, as they have two distinct meanings.
Oracle's treatment of empty strings as NULL is world-renowned for
being insane...

HTH

Mike Mascari

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2
Hi

I understand that null and '' are different, and MySQL and Oracle
functions are confusing, but my question was not about replacing NULL
but replacing Empty strings. These are handled in MySQL/Oracle by the
same functions that do NULL checks.

Is there a standard function in Postgres that replaces Empty strings, as
against a NULL value.

Simon

On Wed, 2004-06-23 at 18:05, Mike Mascari wrote:
Simon Windsor wrote:
Hi

Is there a standard postgres method of replacing empty strings.

In Oracle, nvl handles nulls and empty strings, as does ifnull() in
MySQL, but with postgres coalesce only handles null strings.

If, not is the best solution to create a plpgsql function, ie

CREATE FUNCTION isEmpty (character varying, character varying) RETURNS
character varying


This all depends upon what you mean by handle. Do you want to treat
empty strings as NULL or NULL as empty strings? As you said, you can
treat NULL as empty strings using COALESCE:

SELECT COALESCE(x, '');

You can treat empty strings as NULL

SELECT NULLIF(x, '');

But I'd guess most on this list are wondering why you want to equate
an empty string with NULL, as they have two distinct meanings.
Oracle's treatment of empty strings as NULL is world-renowned for
being insane...

HTH

Mike Mascari

--
Simon Windsor
Email: si***********@cornfield.org.uk
Tel: 01454 617689
Mob: 07960 321599
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Mailscanner thanks transtec Computers for their support.
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3
Simon Windsor <si***********@cornfield.org.uk> writes:
Hi

I understand that null and '' are different, and MySQL and Oracle
functions are confusing, but my question was not about replacing NULL
but replacing Empty strings. These are handled in MySQL/Oracle by the
same functions that do NULL checks.

Is there a standard function in Postgres that replaces Empty strings, as
against a NULL value.


I don't think so, but you could use a CASE clause for this.

-Doug

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #4
On Wed, Jun 23, 2004 at 19:10:05 +0100,
Simon Windsor <si***********@cornfield.org.uk> wrote:
Hi

I understand that null and '' are different, and MySQL and Oracle
functions are confusing, but my question was not about replacing NULL
but replacing Empty strings. These are handled in MySQL/Oracle by the
same functions that do NULL checks.

Is there a standard function in Postgres that replaces Empty strings, as
against a NULL value.


Something like the following may suit your purposes:
coallesce(nullif(inputstring,''), 'replacement value')

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #5
Hi

Thanks, I should have thought of that myself!

Simon
On Wed, 2004-06-23 at 19:43, Bruno Wolff III wrote:
On Wed, Jun 23, 2004 at 19:10:05 +0100,
Simon Windsor <si***********@cornfield.org.uk> wrote:
Hi

I understand that null and '' are different, and MySQL and Oracle
functions are confusing, but my question was not about replacing NULL
but replacing Empty strings. These are handled in MySQL/Oracle by the
same functions that do NULL checks.

Is there a standard function in Postgres that replaces Empty strings, as
against a NULL value.


Something like the following may suit your purposes:
coallesce(nullif(inputstring,''), 'replacement value')

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

--
Simon Windsor
Email: si***********@cornfield.org.uk
Tel: 01454 617689
Mob: 07960 321599
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Mailscanner thanks transtec Computers for their support.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Bedford | last post by:
I've a query where I retrieve 2 codes. select ida, idb from mytable. I've an other table where I've some "labels". id:1 text:shoes id:2 text:socks .....
4
by: Spark | last post by:
Hi, Situation: Need a query to return number of ticket records by month of open in a log table where the ticket open record is older than 24 hours then the ticket pending or ticket closed...
2
by: Jeff Roughgarden | last post by:
I am using a clever product called SQL Expert Pro to optimize problematic SQL statements. It works by generating syntactically identical variations of the original SQL, finding all unique execution...
8
by: xixi | last post by:
when i create a join view like this create view JV104FZ.APJTINM1 (APAM32, APNO20, APQY05, PONO01, PONO05, PONO19, POCD01, POCD13, systimestamp, loginname, id ) as select JV104FZ.APPTINM.APAM32,...
5
by: John Greve | last post by:
Any ideas on why my the result 'green' row column DELTA does not yield -1 for? That is not how I expected COALESCE( ... ) to work. Every other row for DELTA (including 'black') comes out the...
3
by: darrel | last post by:
I have three tables making a many-to-many relationship: Companies CoID | CoName Link CoID | InsID Insurers InsID | InsName
10
by: db2group88 | last post by:
hi, we are using db2 v8.2 EE on windows, with jcc driver. try to create a join view, after that i check the syscat.columns table, for those decimal field use COALESCE method, all the length become...
2
by: Veeru71 | last post by:
We have the following query and is taking considerable longer time to run. select A.*, B.name,B.user_type,C.task_id, c.queue_id, c.workid FROM A, B, C WHERE (B.user_type <2 and B.user_type is...
9
by: leebridgewater | last post by:
Hello Group, I'm new to SQL sever but I have managed to create a SQL based online application and I just have a question that I'm sure someone here will be able to help me with. I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.