Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

what are all the possible values that a blank field in a sql server table could be? Expand / Collapse
Author
Message
Posted Tuesday, October 22, 2013 11:33 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 4:45 PM
Points: 308, Visits: 839
If a sql server table's column values
1. allows nulls
2. originally came from another database
3. occasionally contains the word NULL in it
4. is empty half the time

what are all the conditions that you must evaluate for in the case of a CASE statement in which you handle for that field being 'empty'.

I am currently using the following statement:
(1) CASE WHEN LEN(realname) = 0 and LEN(computer_name) = 0 then email

but I've also used:
(2) CASE WHEN realname IS NULL OR realname = '' and computer_name IS NULL OR computer_name = '' THEN email

WHICH IS BETTER, 1 OR 2?



Post #1507263
Posted Tuesday, October 22, 2013 1:16 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:02 PM
Points: 13,182, Visits: 12,669
polkadot (10/22/2013)
If a sql server table's column values
1. allows nulls
2. originally came from another database
3. occasionally contains the word NULL in it
4. is empty half the time

what are all the conditions that you must evaluate for in the case of a CASE statement in which you handle for that field being 'empty'.

I am currently using the following statement:
(1) CASE WHEN LEN(realname) = 0 and LEN(computer_name) = 0 then email

but I've also used:
(2) CASE WHEN realname IS NULL OR realname = '' and computer_name IS NULL OR computer_name = '' THEN email

WHICH IS BETTER, 1 OR 2?





According to your rules neither of these is accurate. They will both allow columns with the string literal 'NULL'. If you are ok with ignoring rule #3 than I would do a slightly different version of (2).

Case when ISNULL(realname, '') = '' AND ISNULL(computer_name, '') = '' then email



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1507309
Posted Tuesday, October 22, 2013 1:25 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 12:19 PM
Points: 4,360, Visits: 9,543
Sean Lange (10/22/2013)
polkadot (10/22/2013)
According to your rules neither of these is accurate. They will both allow columns with the string literal 'NULL'. If you are ok with ignoring rule #3 than I would do a slightly different version of (2).

Case when ISNULL(realname, '') = '' AND ISNULL(computer_name, '') = '' then email



To handle the word NULL, couldn't we do this?

Case when ISNULL(NULLIF(realname, 'NULL'), '') = '' AND ISNULL(NULLIF(computer_name, 'NULL'), '') = '' then email




Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #1507320
Posted Tuesday, October 22, 2013 1:30 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:02 PM
Points: 13,182, Visits: 12,669
Jeffrey Williams 3188 (10/22/2013)
Sean Lange (10/22/2013)
polkadot (10/22/2013)
According to your rules neither of these is accurate. They will both allow columns with the string literal 'NULL'. If you are ok with ignoring rule #3 than I would do a slightly different version of (2).

Case when ISNULL(realname, '') = '' AND ISNULL(computer_name, '') = '' then email



To handle the word NULL, couldn't we do this?

Case when ISNULL(NULLIF(realname, 'NULL'), '') = '' AND ISNULL(NULLIF(computer_name, 'NULL'), '') = '' then email




Nicely done!!! I think that should do it.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1507323
Posted Thursday, October 24, 2013 3:02 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 4:45 PM
Points: 308, Visits: 839
Thank you. By combining ISNULL and NULLIF functions it's the most elegant handling of the true NULL and text NULL scenario I've seen. Few months back I was doing it using AND, which made for very long statement. This is better.
WHEN ISNULL(NULLIF(realname, 'NULL'), '') = '' AND ISNULL(NULLIF(computer_name, 'NULL'), '') = '' then email
Post #1508282
Posted Thursday, October 24, 2013 6:30 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:46 AM
Points: 3,422, Visits: 5,364
Post retracted... not enough coffee this morning.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1508305
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse