If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Is it possible to return a real value instead of a null value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-23-08, 17:04
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Is it possible to return a real value instead of a null value

Using SQL, Is it possible to echo a real value like "Pending" if and when a field is null? I have 5 columns in my result set. 3 of these 5 will have null values for a time until the user updates the table. In my app code, I would have to test for null values and then write the proper code to echo something in its place until the value was available. Is it possible to do this with SQL? It would save me a small headache if this was possible.

Thanks,
Frank
Reply With Quote
  #2 (permalink)  
Old 12-23-08, 18:17
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Code:
select ifnull( status3, "Pending" )
from MyTable
or look at the docs
Reply With Quote
  #3 (permalink)  
Old 12-23-08, 18:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i prefer to use COALESCE instead of IFNULL

COALESCE is standard SQL, whereas IFNULL is proprietary to mysql

in addition, COALESCE can take multiple parameters, so that, when necessary, you can write expressions like

... COALESCE(column1,column2,column3,'default')

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 12-23-08, 19:13
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
In my app code, I would have to test for null values and then write the proper code to echo something in its place until the value was available. Is it possible to do this with SQL? It would save me a small headache if this was possible.
The other option might be to just store the value "Pending" rather than storing NULL and later decoding it.
Reply With Quote
  #5 (permalink)  
Old 12-23-08, 19:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by mike_bike_kite
The other option might be to just store the value "Pending" rather than storing NULL and later decoding it.
that wouldn't work if it's a numeric column

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-24-08, 06:38
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by r937
that wouldn't work if it's a numeric column
I often come across databases where each record has a status value of 1,2,3 and perhaps -2 and -5 are then added as error conditions. Then perhaps every year some new developer will add some new codes like 51 and 52 etc. Some of these new codes will invariably be almost identical to some existing forgotten codes. I personally dislike these integer codes for statuses but figure people put them in for various reasons:
  • Because it saves space - it might be noticeable in a db with billions of records but invariably, in most db's, it hardly makes any saving. The code on the other hand becomes bloated and unreadable - each sproc will be full of code like where status_id in ( 1,3,-5,0 ) which means nothing to man nor beast.
  • Because it goes faster - I suppose the indexes will be more compact with more more nodes on a given part of the b-tree but to be honest I doubt if you'd notice any difference in real life. Also if you have to join with another table to be able to decode your integer status values then you've probably lost any perceived gain.
  • Because they can add extra functionality to their codes - ie where status_id < 0. It might work while the original designer is still working there but it quickly degenerates into unreadable code. A better way might be to use flags within the status table.
  • Because it looks more like a proper database - this is the funniest reason to me but I suspect that's why most designers do it.
There may be a few cases where integer status id's might be better but, for most of the databases I've come across, they'd of been far better making their data readable and the code clearer by avoiding using integer status values.

Mike
Reply With Quote
  #7 (permalink)  
Old 12-24-08, 06:54
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Natural vs surrogate!

Nice points Mike
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 12-24-08, 09:39
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Thanks guys. As always, I appreciate the help and direction. COALESCE sounds like my best option.

Reply With Quote
  #9 (permalink)  
Old 12-24-08, 10:05
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 791
Rudy, you be da man!! That COALESCE function is perrrfect. Using it will save me from coding copious amounts of application code in the future.

Thank you!!!!
Frank
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On