Solved

Looping nvarchar(max) delimited content and detecting newline

Posted on 2014-04-29
5
301 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard 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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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