Solved

Looping nvarchar(max) delimited content and detecting newline

Posted on 2014-04-29
5
294 Views
Last Modified: 2014-05-05
Looking for an example of looping nvarchar(max) delimited content and detecting newline.  Once a new line is detected ill need to do some validation of that lines content before moving onto the next.

Thank you in advance.
Keith
0
Comment
Question by:westbergk
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40030299
You can use this procedure to split on a char.

select item into #temp
from dbo.fn_txt_split ('1,,2,,3',',,')  --replace the '1,,2,,3' with your text, ',,' with the new line characters you're using.

-- now holds the rows to validate.
select * from #temp

note that you can do '\r\n' as the characters to split on if needed.  The splitter doesn't have to be just one character.




USE [IVCILocal]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_Txt_Split]    Script Date: 04/29/2014 14:24:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




Create Function [dbo].[fn_Txt_Split]( 
    @sInputList varchar(8000) -- List of delimited items 
  , @Delimiter char(1) = ',' -- delimiter that separates items 
) 
RETURNS @list table (Item varchar(8000)) 
as begin 
DECLARE @Item Varchar(8000) 
  
  

WHILE CHARINDEX(@Delimiter,@sInputList,0) <> 0 
BEGIN 
SELECT 
@Item=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@Delimiter,@sInputList,0 
)-1))), 
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@Delimiter,@sInputList,0)+1,LEN(@sInputList)))) 
  
IF LEN(@Item) > 0 
INSERT INTO @List SELECT @Item 
  
END 

  
IF LEN(@sInputList) > 0 
INSERT INTO @List SELECT @sInputList -- Put the last item in 
  
return 
END 



GO

Open in new window

0
 
LVL 1

Author Comment

by:westbergk
ID: 40030435
So if i had to tinker with each line in some way, how would you imagine it playing out if the nvarchar(max) contained the following.  To keep it simple lets say i just want to get the character count of each line.

a,b,c
1,2,3
9,8,7

thanks again
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40030462
you could do
select len(item) from #temp

if you needed to validate a certain length:
if (select count(*) from #temp where len(item) != <MYLENGTH) >)  0
  -- error
else
  -- procede

Open in new window

if you need to process line by line I would do something like:

declare @line varchar(max)

while (select count (*) from #temp) > 0
begin
   set @line = (select top 1 item from #temp)
   ... do stuff
   set rowcount 1  --ensure there's only one row getting deleted
   delete from #temp
   set rowcount 0 -- go back to affecting all rows
end

Open in new window

0
 
LVL 6

Expert Comment

by:Dulton
ID: 40030473
Here's a good option, it's not a 101 solution, as it involves SQLCLR.
String splitting is one of the places a CLR function will blow away t-sql in terms of performance. Even if you don't implement it, it's a good read.

I've used a tweaked version of Adam Machanic's SQLCLR solution for several years now:
http://sqlblog.com/blogs/adam_machanic/archive/2009/04/28/sqlclr-string-splitting-part-2-even-faster-even-more-scalable.aspx

It's fast.

After you create and load the clr assembly, it's called like any other table-valued function. I've found that you can order the items coming out of the function in the order in which they were in the string

SELECT ssm.item
            ,ROW_NUMBER() OVER(ORDER BY @MyDelimiter) AS  [SplitOrder]
 FROM dbo.SplitString_Multi(@MyNvarcharmaxCol,@MyDelimiter) as ssm

By having the items numbered, with a few nested CTE's you can do set-based manipulation on your dataset, rather than RBAR, cursors. If you're just manipulating the dataset, it can probably be done as a set-based solution.
0
 
LVL 1

Author Closing Comment

by:westbergk
ID: 40043576
Thank you guys for the help.  This worked like a charm.  =)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now