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

Best way to handle Null values Expand / Collapse
Author
Message
Posted Thursday, October 11, 2012 3:11 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 27, 2013 11:28 AM
Points: 12, Visits: 82
I need to write a script that I carry a variable for the order number. However, that field may be null for several records. How do I created a sql statement that knows what to use? Simplified example code:

Declare
@OrdNbr CoNumType

Select order_nbr, field1, field2, field3
from table
where order_nbr = @Ord_nbr

So, if the value @ordNbr is null I need to temporarily add a value like 'xxxxx' so that record is the only one to print when the user hits the print button. If it is not null then use the value in the field.

I did not know if it was possible or if I could somehow use a case statement....any help would be appreciated.
Post #1371796
Posted Thursday, October 11, 2012 3:18 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,067, Visits: 11,903
where order_nbr = isnull(@Ord_nbr, 'xxxx') maybe????

_______________________________________________________________

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 #1371799
Posted Thursday, October 11, 2012 3:35 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 27, 2013 11:28 AM
Points: 12, Visits: 82
It works too a point, but it pulls all the records. If I substitute @Ord_Nbr ='12345' the script still pulls all the records and not the record that is = to '12345'
Post #1371804
Posted Thursday, October 11, 2012 3:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,067, Visits: 11,903
Cheryl McLaughlin-385812 (10/11/2012)
It works too a point, but it pulls all the records. If I substitute @Ord_Nbr ='12345' the script still pulls all the records and not the record that is = to '12345'


Do you want all rows where is it either '123345' OR NULL?

That would be something like:
where order_nbr = @Ord_nbr
OR order_nbr is null



_______________________________________________________________

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 #1371806
Posted Thursday, October 11, 2012 7:47 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:13 AM
Points: 3,998, Visits: 6,053
If I understand you, you want to have a default value to be returned whenever no value is specified.

Select order_nbr, field1, field2, field3
from table
where order_nbr = isnull(@Ord_nbr,'xxxxxx')



__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #1371856
Posted Thursday, October 11, 2012 7:57 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 6:55 PM
Points: 21,204, Visits: 14,889
The Dixie Flatline (10/11/2012)
If I understand you, you want to have a default value to be returned whenever no value is specified.

Select order_nbr, field1, field2, field3
from table
where order_nbr = isnull(@Ord_nbr,'xxxxxx')



That's the way I understand it too.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1371860
Posted Thursday, October 11, 2012 9:23 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,067, Visits: 11,903
The Dixie Flatline (10/11/2012)
If I understand you, you want to have a default value to be returned whenever no value is specified.

Select order_nbr, field1, field2, field3
from table
where order_nbr = isnull(@Ord_nbr,'xxxxxx')



That is exactly what my response contained but apparently that isn't quite 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 #1371877
Posted Monday, October 29, 2012 4:04 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:13 AM
Points: 3,998, Visits: 6,053
I know, Sean. And I can't understand why that isn't it. It would be nice if the OP came back and told us what the final solution to their problem was.


__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #1378498
Posted Monday, October 29, 2012 4:06 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:13 AM
Points: 3,998, Visits: 6,053
Cheryl McLaughlin-385812 (10/11/2012)
It works too a point, but it pulls all the records. If I substitute @Ord_Nbr ='12345' the script still pulls all the records and not the record that is = to '12345'



This makes me think that the problem lies elsewhere. Perhaps in an OR section of her WHERE clause.


__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #1378500
Posted Monday, October 29, 2012 4:17 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: Yesterday @ 11:22 PM
Points: 3,325, Visits: 7,174
For what I understood, there's one condition missing in the previous solutions.
Here's my guess (since I have nothing to test on)

Declare
@OrdNbr CoNumType

Select ISNULL( order_nbr, 'xxxxxx') order_nbr, field1, field2, field3
from table
where order_nbr = @Ord_nbr
OR( order_nbr IS NULL AND @Ord_nbr IS NULL)




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1378504
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse