Solved

SQL Stored Procedure - How to concatenate two colums based on the value of one of them.

Posted on 2013-05-24
2
604 Views
Last Modified: 2013-05-24
I have the need of a stored procedure that can concatenate two columns only if both are populated.  

Here is the scenario:

There are school records where there is a school name and in some cases a parent school name.  I need the stored procedure to return the columns concatenated if both names are populated.  This is what I am looking for:

Example 1 -
School Name = Secondary School
Parent School Name = NULL
Output = Secondary School

Example 2 -
School Name = Secondary School
Parent School Name = Parent School
Output = Secondary School (Parent School)

Here is the SQL that currently returns the two columns:

SELECT
      E.ApplicantId,,
      S.SchoolName AS SchoolName,
      (SELECT SchoolName FROM CFG_Schools WHERE SchoolId = S.ParentSchoolId) AS ParentSchoolName
      
FROM
      APP_Education E
      LEFT JOIN CFG_Schools S ON E.SchoolId = S.SchoolId

Someone mentioned that I should be able to get my result using a CASE statement.   I am not familiar with CASE statements but doing some research it appears that might be the way to go.  If not what would be the solution.

Your assistance would help me solve this issue as well as educate me further in SQL stored procedures.

THANK YOU!!!!
0
Comment
Question by:skinsfan99
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39195662
yes you can do that with case as below

SELECT
      E.ApplicantId,
      S.SchoolName AS SchoolName, 
	  SELECT SchoolName FROM CFG_Schools WHERE SchoolId = S.ParentSchoolId) AS ParentSchoolName
      CASE INULL((SELECT SchoolName FROM CFG_Schools WHERE SchoolId = S.ParentSchoolId),'')
		WHEN '' THEN SchoolName 
		ELSE SchoolName + ' ('+ (SELECT SchoolName FROM CFG_Schools WHERE SchoolId = S.ParentSchoolId)  + ')'
		END AS [Output]
FROM
      APP_Education E
      LEFT JOIN CFG_Schools S ON E.SchoolId = S.SchoolId

Open in new window

0
 

Author Closing Comment

by:skinsfan99
ID: 39195691
Perfect!!!  THANK YOU!!!!
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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

930 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

9 Experts available now in Live!

Get 1:1 Help Now