Solved

SQL - How to concatenate a string with condition

Posted on 2012-03-16
4
272 Views
Last Modified: 2012-03-16
I have a table tbl1

Name | account code | Type  | detail
-----------------------------------------------------------------
P1       | 12345              | Open | detail1
P2       | 12341              | Close |

I would like to do a Select that will selectively concatenate the information into a description if the information is available.
e.g

for first record
P1 - 12345 - Open (detail1)
but for second record, no detail being concatenated since it is not available.
P1 - 12341 - Close


How will I do that?
0
Comment
Question by:tommym121
[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
  • 3
4 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 37731152
It would be something like:


select
      case when [type] = 'closed'
                  then (select Name+account+code+[TYPE])
                  else (select Name+account+code+[TYPE]+detail)
             end as col1
from tbl1
0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 37731156
OK - added the dashes and cast in case account column is numeric:


select
      case when [type] = 'closed'
                  then (select Name+'-'+cast(account as sysname)+'-'+code+'-'+[TYPE])
                  else (select Name+'-'+cast(account as sysname)+'-'+code+'-'+[TYPE]+'-'+detail)
             end as col1
from tbl1
0
 
LVL 40

Expert Comment

by:lcohan
ID: 37731158
Darn..."close" not closed so once again - sorry:


select
      case when [type] = 'close'
                  then (select Name+'-'+cast(account as sysname)+'-'+code+'-'+[TYPE])
                  else (select Name+'-'+cast(account as sysname)+'-'+code+'-'+[TYPE]+'-'+detail)
             end as col1
from tbl1
0
 

Author Closing Comment

by:tommym121
ID: 37731838
Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

733 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