Solved

IF clause in an SQL SELECT statement?

Posted on 2004-04-05
9
1,992 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
 
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now