Solved

SQL Server - conditional statement in the select list

Posted on 2011-03-10
3
432 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
Comment Utility
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
Comment Utility
or this.
select FirstField,ISNULL(FirstField+':'+SecondField,FirstField) AS SecondField

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility

declare @SQL

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

--print @SQL
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

762 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