PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2003, 04:33 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Query help with string concatenation and NULL

I have a query that looks like

PHP Code:
 UPDATE emp_skill SET comments 'REQUESTING UPDATE ' || comments WHERE emp_skill.signon_id 'V935906' AND ort_skill.ort_id '57' AND ort_skill.sk_id emp_skill.sk_id AND emp_skill.pre_init 'NOTREQ' 


which does what I want (appends REQUESTING UPDATE to whatever is in comments) if emp_skill.comments is not null, however if it is null then the update does nothing even though phpPgAdmin tells me that the expected number of rows were affected. I was just wondering what's the best way of getting around this (without having to run another query that checks for NULLs) and also, why the affected number of rows is greater than 0 if nothing actually changes. Is it because concatenating a string with NULL yields NULL?

This is on RH 7.2 on pg 7.1.2 (yeah I know I need to upgrade )

Thanks,
-b
__________________
PostgreSQL, it's what's for dinner...

Reply With Quote
  #2  
Old March 19th, 2003, 05:00 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
Indeed, since NULL is by definition not a value, there is no way to concatenate anything to it. Let's just log into our psql terminal:
Code:
mydb=# SELECT NULL;
 ?column?
----------

(1 row)

mydb=# SELECT NULL || 'huh';
 ?column?
----------

(1 row)

mydb=# SELECT '' || 'huh';
 ?column?
----------
 huh
(1 row)

So you see that concatenating a string to an empty string works, but concatenating to NULL does not work. You have two choices:

1. Make this a NOT NULL column, with a default value of '' (empty string), Thus, you can effectively treat an empty string as "nothing" for your programming logic, but you can still concatenate a value to it.

2. Add some logic in your query (perhaps using CASE) to check for a NULL, and if it encounters a null, it replaces, instead of concatenating.

My philosophy is to never allow NULL in my table definitions. I agree with C.J. Date and a few other database thinkers, who believe it was a mistake for the SQL standard to support NULL at all. For relational database operations, it introduces all kinds of "gotchas" like the one you encountered above, without providing any great value, other than complicating things.

There are a great many potential problems in more advanced queries, if nulls are involved. I won't go into detail here, but try it: play with different kinds of joins, aggregate queries, WHERE clauses etc... with and without nulls and you will see how nulls can affect the query results. sometimes providing unexpected results, especially for statistics.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #3  
Old March 19th, 2003, 05:46 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Thanks!

I'll try looking into CASE here. BTW upon your recommendations I picked up Date's, An Introduction to Database Systems and while it isn't the easiest reading I've done, I've found it pretty valuable reference in the few chapters I've read so far.

UPDATE
CASE worked like a charm here's my new query in case it helps anyone else:

PHP Code:
 UPDATE emp_skill SET comments = CASE WHEN comments IS NULL THEN 'REQUESTING UPDATE CUZ ' ELSE 'REQUESTING UPDATE CUZ ' || comments END WHERE emp_skill.signon_id 'V935906' AND ort_skill.ort_id '57' AND ort_skill.sk_id emp_skill.sk_id AND emp_skill.pre_init 'NOTREQ' 


Thanks again,
-b

Last edited by bcyde : March 19th, 2003 at 05:58 PM.

Reply With Quote
  #4  
Old March 19th, 2003, 09:21 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
Re: Thanks!

Quote:
Originally posted by bcyde
I'll try looking into CASE here. BTW upon your recommendations I picked up Date's, An Introduction to Database Systems and while it isn't the easiest reading I've done, I've found it pretty valuable reference in the few chapters I've read so far.

I just started reading Foundation for Future Database Systems (The Third Manifesto) by Date. Very interesting reading, hazy place where object-oriented programming and relational data meet.

Quote:

CASE worked like a charm

Glad it worked for you. PostgreSQL is an awesome tool, isn't it ?

Cheers

Reply With Quote
  #5  
Old March 25th, 2003, 11:40 AM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
I see now that Oracle actually treats NULLs incorrectly, thus encouraging all sorts of bad programming logic. Here is also another example of the kind of trouble you can get into by depending on Nulls; from the PostgreSQL mailing lists:

Quote:
> Hi,
>
> I work with Oracle and to it,
> a empty quote concatenation is equal NULL.
>
> ORACLE:
> select 1 from dual where '' is null
> Result: 1
> select 1 from dual where trim(' ') is null
> result: 1
> insert into tabela values ('')
> result: insert a value null in a table

Yes, Oracle is wrong here. They made a mistake a long time ago, and since
everyone started coding as though '' = NULL, they couldn't change it.
Maybe someday they will deprecate this behaviour then finally code it out
of their database, but I kinda doubt that.

> POSTGRE
> select 1 where '' i snull
> Result: NULL
> select 1 where trim(' ') is null
> result: NULL
> insert into tabela values ('')
> result: insere '' in table

Yes, because NULL != NULL != '' != 0 because NULL is unknown, so in set
theory it can't be assumed to be equal to anything, even itself.

> We can't change all ours source code
> bacause there are very occurrences.

Wow. You should probably review all your code if folks were treating ''
like NULL then they've probably made some other serious mistakes in how
they treat data. I'd definitely check your data constraints, NOT NULL
stuff, things like that.


Reply With Quote
  #6  
Old March 25th, 2003, 12:53 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Thx for the heads up...

Yeah, I've been going through some of my predecessor's code trying to find spots where he may have used '' and NULL interchangeably. Some hard to diagnose bugs have been squashed now . Nothing against my predecessor as he did good, work, but I think he probably just picked up on the NULL / '' differences a little bit later on in development.

Too bad, there's no easy way to distinguish between NULL and '' when viewing data in phppgadmin (except of course by selecting based on NULL and ''), I could've caught onto this sooner .

BTW, have you started posting to dbforums as well? I thought I saw a post by you there.

-b

Reply With Quote
  #7  
Old March 25th, 2003, 12:59 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
Re: Thx for the heads up...

Quote:
Originally posted by bcyde
BTW, have you started posting to dbforums as well? I thought I saw a post by you there.

Well.... every once in awhile, I troll through the other forums to fix all the unanswered PostgreSQL questions. Just doing my good duty as a netizen .

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Query help with string concatenation and NULL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT