Solved

IF clause in an SQL SELECT statement?

Posted on 2004-04-05
9
1,980 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
Comment Utility
Try this

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


Joshua


0
 
LVL 1

Expert Comment

by:jmcraig
Comment Utility
P.S.

Table1 is your table name of course
0
 
LVL 3

Author Comment

by:fcfang
Comment Utility
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
Comment Utility
Try this then


SELECT  ISNULL(NULLIF (Nickname, ''), FirstName) AS Nickname, LastName FROM  Table1
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 39

Expert Comment

by:appari
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
execute a MS SQL script as a schedule SQL job 72 96
Select2 jquery help 9 41
Sql query 34 14
Azure SQL DB? 3 11
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
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 …
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

763 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

6 Experts available now in Live!

Get 1:1 Help Now