Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Continue until NULL in SQL Server

Posted on 2013-11-20
2
Medium Priority
?
381 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 2000 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

661 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