Solved

SQL Server - conditional statement in the select list

Posted on 2011-03-10
3
438 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
[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
  • 2
3 Comments
 
LVL 41

Expert Comment

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

Open in new window

0
 
LVL 41

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:Ephraim Wangoya
ID: 35101482

declare @SQL

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

--print @SQL
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

726 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