Solved

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

Posted on 2013-05-24
2
607 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
[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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IF SQL Query 12 28
Upgrading to SQL Server 2015 Express 2 31
sql server cross db update 2 20
add stored proc on publlication 4 20
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

756 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