Avatar of JackW9653
JackW9653
Flag for United States of America asked on

Trailing String Characters in SQL Statement

Hi Experts,

I'm trying to compile a list of ';' separated values from 10 fields in a SQL table but I'm left with ';' at the end of my string. Here's my query:

SELECT STUFF(
(SELECT  '; '+ SUBSTRING(Alert1,CHARINDEX('^',Alert1)+1, LEN(Alert1))+ '; ' +
      COALESCE(SUBSTRING(Alert2, CHARINDEX('^', ALERT2)+1, LEN(Alert2)),'') + '; ' +
      COALESCE(SUBSTRING(Alert3, CHARINDEX('^', ALERT3)+1, LEN(Alert3)),'') + '; ' +
      COALESCE(SUBSTRING(Alert4, CHARINDEX('^', ALERT4)+1, LEN(Alert4)),'') + '; ' +
      COALESCE(SUBSTRING(Alert5, CHARINDEX('^', ALERT5)+1, LEN(Alert5)),'') + '; ' +
      COALESCE(SUBSTRING(Alert6, CHARINDEX('^', ALERT6)+1, LEN(Alert6)),'') + '; ' +
      COALESCE(SUBSTRING(Alert7, CHARINDEX('^', ALERT7)+1, LEN(Alert7)),'') + '; ' +
      COALESCE(SUBSTRING(Alert8, CHARINDEX('^', ALERT8)+1, LEN(Alert8)),'') + '; ' +
      COALESCE(SUBSTRING(Alert9, CHARINDEX('^', ALERT9)+1, LEN(Alert9)),'') + '; ' +
      COALESCE(SUBSTRING(Alert10,CHARINDEX('^',Alert10)+1, LEN(Alert10)),'')
    FROM tblPatientData
      WHERE CaseNumber = '10223001'
    FOR XML PATH ('')),1,1,'') AS RESULTS

And Results:

 EPILEPSY; RISK OF FALLS; DIZZINESS; HIV/AIDS; A-FIB      ; ; ; ; ;

It's highly doubtful any patient will exceed 5 Alerts so getting rid of the trailing ';' is imperative. I've just ran out of time on this one.

Thanks in advance,

JackW9653
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
JackW9653

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JackW9653

ASKER
Good idea; here's the finished query:

SELECT STUFF(
(SELECT  '; '+ SUBSTRING(Alert1,CHARINDEX('^', Alert1)+1, LEN(Alert1))+ '; ' +
      CASE WHEN ISNULL(Alert2, '') <> ''THEN COALESCE(SUBSTRING(Alert2, CHARINDEX('^', ALERT2)+1, LEN(Alert2)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert3, '') <> ''THEN COALESCE(SUBSTRING(Alert3, CHARINDEX('^', ALERT3)+1, LEN(Alert3)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert4, '') <> ''THEN COALESCE(SUBSTRING(Alert4, CHARINDEX('^', ALERT4)+1, LEN(Alert4)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert5, '') <> ''THEN COALESCE(SUBSTRING(Alert5, CHARINDEX('^', ALERT5)+1, LEN(Alert5)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert6, '') <> ''THEN COALESCE(SUBSTRING(Alert6, CHARINDEX('^', ALERT6)+1, LEN(Alert6)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert7, '') <> ''THEN COALESCE(SUBSTRING(Alert7, CHARINDEX('^', ALERT7)+1, LEN(Alert7)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert8, '') <> ''THEN COALESCE(SUBSTRING(Alert8, CHARINDEX('^', ALERT8)+1, LEN(Alert8)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert9, '') <> ''THEN COALESCE(SUBSTRING(Alert9, CHARINDEX('^', ALERT9)+1, LEN(Alert9)),'') + '; ' ELSE '' END +
      CASE WHEN ISNULL(Alert10, '') <> ''THEN COALESCE(SUBSTRING(Alert10,CHARINDEX('^',Alert10)+1, LEN(Alert10)),'') ELSE '' END
    FROM tblPatientData
      WHERE CaseNumber = '10223001'
    FOR XML PATH ('')),1,1,'') AS RESULTS

Thanks for your help.

JackW9653
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23