Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trailing String Characters in SQL Statement

Posted on 2013-12-02
2
Medium Priority
?
387 Views
Last Modified: 2013-12-02
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
0
Comment
Question by:JackW9653
[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 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39690564
<really generalizing here>

Would probably be a good idea to validate that the columns are not null before the ;
Something like (repeat for each Alert1..10)...
CASE WHEN ISNULL(Altert7, '') <> '' THEN {your expression here} + ';' ELSE '' END + 

Open in new window

0
 

Author Closing Comment

by:JackW9653
ID: 39690992
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
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

610 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