Solved

SQL - How to concatenate a string with condition

Posted on 2012-03-16
4
260 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
  • 3
4 Comments
 
LVL 39

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 39

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 39

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# SQL BULK INSERT CLASS 5 36
SQL Connection (Error 18456) 14 36
add 1 to a field for 100 rows 11 24
Update in Sql 7 8
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 …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
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.

746 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

8 Experts available now in Live!

Get 1:1 Help Now