Link to home
Create AccountLog in
Avatar of skinsfan99
skinsfan99Flag for United States of America

asked on

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

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!!!!
ASKER CERTIFIED SOLUTION
Avatar of Surendra Nath
Surendra Nath
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of skinsfan99

ASKER

Perfect!!!  THANK YOU!!!!