Solved

IF clause in an SQL SELECT statement?

Posted on 2004-04-05
9
2,002 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
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.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 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