Solved

Looping nvarchar(max) delimited content and detecting newline

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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