October 11, 2012 at 3:11 pm
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.
October 11, 2012 at 3:18 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 11, 2012 at 3:35 pm
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'
October 11, 2012 at 3:38 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 11, 2012 at 7:47 pm
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? Everybody look what's going down. -- Stephen Stills
October 11, 2012 at 7:57 pm
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, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
October 11, 2012 at 9:23 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 29, 2012 at 4:04 pm
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? Everybody look what's going down. -- Stephen Stills
October 29, 2012 at 4:06 pm
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? Everybody look what's going down. -- Stephen Stills
October 29, 2012 at 4:17 pm
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)
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply