Solved

Continue until NULL in SQL Server

Posted on 2013-11-20
2
368 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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

803 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