Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql select record as one long string 21 24
always on switch back after failover 2 35
Deal with apostrophe in stored procedures 8 43
Job - date manual 1 22
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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

832 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