Solved

SQL Server - conditional statement in the select list

Posted on 2011-03-10
3
435 Views
Last Modified: 2012-06-21
I have a query that needs to build a display name for a field as part of the select list.  The display name will consist of two database fields from the tables in the query.  The first field will always be displayed.  But the second field could potentially be null.  If the second field is null then I just want to display the first field.  But if the second field is not null then I want to display the first field a colon and then the second field and have it returned as part of the select list.

Thanks
0
Comment
Question by:JosephEricDavis
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35101081
Try like this.
select FirstField,COALESCE(FirstField+':'+SecondField,FirstField) SecondField

Open in new window

0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35101083
or this.
select FirstField,ISNULL(FirstField+':'+SecondField,FirstField) AS SecondField

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35101482

declare @SQL

select @SQL = FirstField + ISNULL(',' + SecondField, '')

--print @SQL
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL for SS2000 -- get characters until a digit 6 19
SQL Exceptions 3 35
Need to update TableA to TableB 6 33
Sql Join Problem 2 18
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

947 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

20 Experts available now in Live!

Get 1:1 Help Now