Solved

IF clause in an SQL SELECT statement?

Posted on 2004-04-05
9
2,024 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

 
LVL 1

Accepted Solution

by:
jmcraig earned 450 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

617 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