Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IF clause in an SQL SELECT statement?

Posted on 2004-04-05
9
Medium Priority
?
2,032 Views
Last Modified: 2008-02-18
Hi folks,

I have a SQL 2000 database with columns
FirstName, Lastname and Nickname. I'd like to create a view that returns the list such that the Nickname is returned as the first column if that row contains one a nickname or the FirstName if a Nickname doesn't exist. The kicker is I'd like the Nickname/FirstName first column to be the primary sort.

ie

James, Burton, Jim
Sally, Bossie,<NULL>
William, Inky, Bill

would return
Bill, Inky
Jim, Burton
Sally, Bossie

anyone know how I might accomplish this in a select statement?  Thanks very much in advance.
0
Comment
Question by:fcfang
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 1

Expert Comment

by:jmcraig
ID: 10762240
Try this

SELECT     COALESCE (Nickname, FirstName) AS Nickname, LastName
FROM         Table1


Joshua


0
 
LVL 1

Expert Comment

by:jmcraig
ID: 10762244
P.S.

Table1 is your table name of course
0
 
LVL 3

Author Comment

by:fcfang
ID: 10762316
My apologies, Joshua, my original question was flawed. I also have rows in the DB that are non-null for the Nickname but is an empty string. Once I get the answer, I'll make sure you get some of the points.

So to re-state the question...

I have a SQL 2000 database with columns
FirstName, Lastname and Nickname. I'd like to create a view that returns the list such that the Nickname is returned as the first column if that row contains one a nickname or the FirstName if a Nickname doesn't exist. The kicker is I'd like the Nickname/FirstName first column to be the primary sort.

ie

James, Burton, Jim
Sally, Bossie,<NULL>
William, Inky, Bill
John, Olsen, "" <-- an empty string
Kyle, Johans,""


would return
Bill, Inky
Jim, Burton
John, Olsen
Kyle, Johans
Sally, Bossie

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
jmcraig earned 1800 total points
ID: 10762559
Try this then


SELECT  ISNULL(NULLIF (Nickname, ''), FirstName) AS Nickname, LastName FROM  Table1
0
 
LVL 39

Expert Comment

by:appari
ID: 10762835
use case structure. your query should be something like this

SELECT     case when isnull(Nickname,'')='' then FirstName else NickName) end AS Nickname, LastName
FROM         yourtable
0
 
LVL 39

Expert Comment

by:appari
ID: 10762837
mistake in prev query, extra paranthasis,

SELECT     case when isnull(Nickname,'')='' then FirstName else NickName end AS Nickname, LastName
FROM         yourtable
0
 
LVL 2

Expert Comment

by:johnfarragher
ID: 10767278
select FirstName + ', ', Lastname
from YOURTABLE
where Nickname is null

union

select Nickname + ', ', Lastname
from YOURTABLE
where Nickname is not null
0
 
LVL 2

Expert Comment

by:johnfarragher
ID: 10767288
select FirstName + ', ' as First, Lastname
from YOURTABLE
where Nickname is null

union

select Nickname + ', ' as First, Lastname
from YOURTABLE
where Nickname is not null
0
 
LVL 3

Author Comment

by:fcfang
ID: 10769023
Thanks very much to all of you for responding. Joshua's solution was a somewhat simpler and more elegant though it was nice to be able to see how I could use the CASE WHEN statement in a SELECT query from appari.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

885 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