1

I'm not sure of a better way to word this, but say I have a table

name,    homephone,   fax,    mobile
========================================
bob,     123,         456,    999
chris,   null,        890,    null

I'm trying to create a SQL statement that will get me something like this

name,     phone
================
bob,      123
bob,      456
bob,      999
chris,    890
0

2 Answers 2

6

How about using UNION? Say your table is called Directory. Something like:

SELECT name, homephone AS phone FROM Directory WHERE homephone IS NOT NULL
UNION
SELECT name, fax AS phone FROM Directory WHERE fax IS NOT NULL
UNION
SELECT name, mobile AS phone FROM Directory WHERE mobile IS NOT NULL
Sign up to request clarification or add additional context in comments.

Comments

3
select T.name, P.phone
from YourTale as T
  cross apply (
                select homephone union all
                select fax union all
                select mobile
              ) as P(phone)
where p.phone is not null

1 Comment

+1 I prefer this over Robert's solution, but Robert's is excellent as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.