Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I modify my merge data stored procedure? 3 33
SQL Query (lookup) 8 56
What are the recommended security measures to put in place? 19 83
SQL Query 9 22
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

740 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