Solved

Looping nvarchar(max) delimited content and detecting newline

Posted on 2014-04-29
5
290 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 39

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 39

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

18 Experts available now in Live!

Get 1:1 Help Now