SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Isnull behavior


Isnull behavior

Author
Message
mark.gilbert
mark.gilbert
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 208
Suppose I have 3 fields which are varchar

Area_code
Prefix_code
Last4_code

And suppose the Area_code field is null and the other two field contain 555 and 1212.

The following

select IsNull(Area_code+Prefix_code+Last4_code,'Invalid Phone') from clients

will return 'Invalid Phone' when running in SQL 2000

I'm wondering if the behavior is the same in later versions of SQL Server..

Thanks
John Rowan
John Rowan
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13222 Visits: 4588
Yes. Combining string values with the '+' operator results in a NULL any time one of the values contain a NULL. This is expected behavior. Since NULL means unknown, adding it to an existing string value results in an unknown.

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
mark.gilbert
mark.gilbert
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 208
That is what i wanted to hear! Thanks!
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28525 Visits: 19002
Reading between the lines a bit, it sounds like you might be willing to tolerate the areacode being null. You can still catch invalid phone numbers if you were to push the isnull down one more level. Something like:


select IsNull( isnull(Area_code,'(000)')+Prefix_code+Last4_code,'Invalid Phone')
from clients



would only bounce this as invalid if either of the last two pieces are null.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Rahul Bhosale
Rahul Bhosale
Old Hand
Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)

Group: General Forum Members
Points: 319 Visits: 193
Thats a nice explanation you got !!!

There is also a thing to remember about the ISNULL behavior which is very useful while coding.

Check below link:

http://borngeek.in/understanding-isnulltransact-sql-behavior/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search