Solved

Continue until NULL in SQL Server

Posted on 2013-11-20
2
377 Views
Last Modified: 2013-11-20
Hi experts,

I'm using the following to put ';' after each choice in a multi-column query but it's returning NULL even if there are actual results in the first 9 positions but a NULL in Alert10.

SELECT STUFF(
(SELECT  '; '+ SUBSTRING(Alert1,CHARINDEX('^',Alert1)+1,LEN(Alert1))+ '; ' +
 SUBSTRING(Alert2,CHARINDEX('^',Alert2)+1,LEN(Alert2)) + '; ' +
 SUBSTRING(Alert3,CHARINDEX('^',Alert3)+1,LEN(Alert3)) + '; ' +
 SUBSTRING(Alert4,CHARINDEX('^',Alert4)+1,LEN(Alert4)) + '; ' +
 SUBSTRING(Alert5,CHARINDEX('^',Alert5)+1,LEN(Alert5)) + '; ' +
 SUBSTRING(Alert6,CHARINDEX('^',Alert6)+1,LEN(Alert6)) + '; ' +
 SUBSTRING(Alert7,CHARINDEX('^',Alert7)+1,LEN(Alert7)) + '; ' +
 SUBSTRING(Alert8,CHARINDEX('^',Alert8)+1,LEN(Alert8)) + '; ' +
 SUBSTRING(Alert9,CHARINDEX('^',Alert9)+1,LEN(Alert9)) + '; ' +
 SUBSTRING(Alert10,CHARINDEX('^',Alert10)+1,LEN(Alert10))
    FROM tblPatientData
      WHERE CaseNumber = '10163001'
    FOR XML PATH ('')),1,1,'')

I need a query that will stop when it hits or ignore the NULL. I've played with COALESCE but it kept blowing up on me because of syntax errors. So I'm turning to you guys.

Thanks,

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 9

Accepted Solution

by:
Beartlaoi earned 500 total points
ID: 39663988
Use an ISNULL for each expression:
SELECT STUFF(
(SELECT  '; '+ SUBSTRING(Alert1,CHARINDEX('^',Alert1)+1,LEN(Alert1))+ '; ' + 
 ISNULL(SUBSTRING(Alert2,CHARINDEX('^',Alert2)+1,LEN(Alert2)),'') + '; ' +
 ISNULL(SUBSTRING(Alert3,CHARINDEX('^',Alert3)+1,LEN(Alert3)),'') + '; ' +
 ISNULL(SUBSTRING(Alert4,CHARINDEX('^',Alert4)+1,LEN(Alert4)),'') + '; ' + 
 ISNULL(SUBSTRING(Alert5,CHARINDEX('^',Alert5)+1,LEN(Alert5)),'') + '; ' + 
 ISNULL(SUBSTRING(Alert6,CHARINDEX('^',Alert6)+1,LEN(Alert6)),'') + '; ' +
 ISNULL(SUBSTRING(Alert7,CHARINDEX('^',Alert7)+1,LEN(Alert7)),'') + '; ' +
 ISNULL(SUBSTRING(Alert8,CHARINDEX('^',Alert8)+1,LEN(Alert8)),'') + '; ' + 
 ISNULL(SUBSTRING(Alert9,CHARINDEX('^',Alert9)+1,LEN(Alert9)),'') + '; ' + 
 ISNULL(SUBSTRING(Alert10,CHARINDEX('^',Alert10)+1,LEN(Alert10)),'')
    FROM tblPatientData
      WHERE CaseNumber = '10163001'
    FOR XML PATH ('')),1,1,'')

Open in new window

0
 

Author Closing Comment

by:JackW9653
ID: 39664030
That works, thanks for your quick reply.
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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