[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

T-SQL select different columns based in null value

Posted on 2011-10-05
4
Medium Priority
?
303 Views
Last Modified: 2012-05-12
I have a table containing columns like

id
model
location
num1
num2
num3
...

I need to write a query to select id, model, location, CASE .....

In the CASE, I need to select num1 (if not null)  num2 (if num1 is null) num3 (if num1 and num2 are null)

or say select (if num1 not null) num1
                       (if num1 is null) num2
                       (if num1, num2 are null) num3

To reach my purpose, how should I write the query?

Thanks.
0
Comment
Question by:minglelinch
4 Comments
 
LVL 15

Accepted Solution

by:
tim_cs earned 668 total points
ID: 36920038
COALESCE(Num1, Num2, Num3)
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 668 total points
ID: 36920150
Or ISNULL like below:

select isnull(isnull(num1,num2),num3)
0
 
LVL 61

Assisted Solution

by:HainKurt
HainKurt earned 664 total points
ID: 36920164
case
when num1 is not null then num1
when num2 is not null then num2
when num3 is not null then num3
else 'ALL Null'
end

or

isnull(num1, isnull(num2, num3))


0
 
LVL 1

Author Closing Comment

by:minglelinch
ID: 36920848
Nice answers. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question