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
Solved

SQL - How to concatenate a string with condition

Posted on 2012-03-16
4
267 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 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