June 24, 2013 at 6:39 am
i have table with name orders and field as order_number,order_amt,order_date,order_customer,order_saleperson
i want to write a query that selects all orders save those with zeroes or NULLs in the order_amt field.
how can i do this?
June 24, 2013 at 6:50 am
Hi,
What you're asking is not difficult but it sounds a lot like homework. Could you show us what have you tried?
June 24, 2013 at 5:06 pm
Hint: NULLs cannot be compared with the correct defaul sttings. Check just for <> 0.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 25, 2013 at 2:19 am
Wouldn't you use IS NOT NULL and > 0?
June 25, 2013 at 5:18 am
Dird (6/25/2013)
Wouldn't you use IS NOT NULL and > 0?
No, you would use <> 0
SELECT d.Amt
FROM (SELECT Amt = 1 UNION ALL SELECT 22 UNION ALL SELECT 0 UNION ALL SELECT NULL) d
WHERE d.Amt <> 0
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 25, 2013 at 5:46 am
select * from Orders
where isnull(order_amt,0) =0
or
select * from Orders
where order_amt is null or order_amt = 0
June 25, 2013 at 6:15 am
Ah I see, <> ignores nulls too o:
June 25, 2013 at 6:38 am
Adarsh Chauhan (6/25/2013)
select * from Orderswhere isnull(order_amt,0) =0
or
select * from Orders
where order_amt is null or order_amt = 0
This is exactly the opposite of what the OP wants
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
June 25, 2013 at 7:46 am
Dird (6/25/2013)
Wouldn't you use IS NOT NULL and > 0?
No. NULLs cannot be compared to so they would also be excluded from any check for <> 0. Try it an see. Same thing for checks for "NOT NULL AND NOT BLANK". All you would need is SomeColumn > '' and that covers both because you cannot compare nulls directly unless someone messed with the default settings.
[Edit] Sorry... didn't see your followup post. Yes, "it ignores nulls too". π
--Jeff Moden
Change is inevitable... Change for the better is not.
June 25, 2013 at 7:52 am
NULL is basically unknown, so for example, 1 cant be not equal to NULL as we dont know what NULL is, for that same reason and perhaps more to the point we cant do equal to NULL, where [column] = NULL, it wont return anything.
But we can say ISNULL, that is to say, is unknown.
'Only he who wanders finds new paths'
June 26, 2013 at 8:01 am
Jeff Moden (6/25/2013)
No. NULLs cannot be compared to so they would also be excluded from any check for <> 0. Try it an see.
I would tend to use ISNULL(value, 0) <> 0 anyway, though, because I don't like things that works differently if you use a different comparison operator! π
(By which I mean--if the requirement changed so you had to find all the items where the amount *was* 0 or NULL, you'd get invalid results if you forgot to change the 0 to the ISNULL version--safer to have it that way in the first place).
June 26, 2013 at 8:22 am
paul.knibbs (6/26/2013)
Jeff Moden (6/25/2013)
No. NULLs cannot be compared to so they would also be excluded from any check for <> 0. Try it an see.
I would tend to use ISNULL(value, 0) <> 0 anyway, though, because I don't like things that works differently if you use a different comparison operator! π
(By which I mean--if the requirement changed so you had to find all the items where the amount *was* 0 or NULL, you'd get invalid results if you forgot to change the 0 to the ISNULL version--safer to have it that way in the first place).
err...huh?
_______________________________________________________________
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/
June 26, 2013 at 8:27 am
Not sure how I can explain my reasoning (or lack thereof, probably) more clearly, sorry...
June 26, 2013 at 7:47 pm
paul.knibbs (6/26/2013)
Jeff Moden (6/25/2013)
No. NULLs cannot be compared to so they would also be excluded from any check for <> 0. Try it an see.
I would tend to use ISNULL(value, 0) <> 0 anyway, though, because I don't like things that works differently if you use a different comparison operator! π
(By which I mean--if the requirement changed so you had to find all the items where the amount *was* 0 or NULL, you'd get invalid results if you forgot to change the 0 to the ISNULL version--safer to have it that way in the first place).
So, basically, your logic is to give extra work to the server instead of analyzing any changes on the requirements that might not even happen?
June 27, 2013 at 2:23 am
Is ISNULL such an expensive operation, then? (Genuinely curious, never actually sat down and analysed it)...
Viewing 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply