SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


In-Line Function to split string


In-Line Function to split string

Author
Message
waxb18
waxb18
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 111
Good afternoon gents,

I'm having a slight issue with a function i have created and the way it works.
Some background...

I have a job which has multiple customers, the data is store as such




And i need the in the format below



As it stands the function i have modified/created will return a 2 column values for a single project based on inputting a project number, see below

/****** Object:  UserDefinedFunction [dbo].[SplitList]    Script Date: 05/01/2014 16:51:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter Function [dbo].[SplitCust]
(
@Project Varchar(20),
@CustomerCode [varchar](max),
@CommaDelimiter [varchar](1)
)
RETURNS @Table Table (Project varchar(20), Value [varchar](100))


BEGIN


------------------------------------------------------------------------
------------------------------------------------------------------------

DECLARE @sTemp [varchar](max)


SET @sTemp = ISNULL(@CustomerCode,'')
+ @CommaDelimiter

WHILE LEN(@sTemp) > 0

BEGIN

INSERT INTO @Table


SELECT @Project as Project,
SubString(@sTemp,1,
CharIndex(@CommaDelimiter,@sTemp)-1) as customer


SET @sTemp = RIGHT(@sTemp,
LEN(@sTemp)-CharIndex(@CommaDelimiter,@sTemp))
END

RETURN

END

-----------------------------------------------------------------------------
Declare @Project varchar(10)

Set @Project = ('123')


Select *
from SplitCust

(@Project,

(Select [Customer Code]

FROM dbo.PROJECT

where Code in (@Project)),','

)





What the ultimate outcome i want to achieve is to have a table which has all the projects and customers...

Can you guys point me in the right direction???

Thanks
waxb18
waxb18
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 111
Thinking about it now maybe a SPROC would have been better to use
gbritton1
gbritton1
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1443 Visits: 879
Why not use this splitter?

http://www.sqlservercentral.com/articles/Tally+Table/72993/
Jack Corbett
  Jack Corbett
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45725 Visits: 14925
I'm pretty sure you could use Jeff Moden's DelimitedSplit8K function using CROSS APPLY. Something like this:


SELECT
P.project AS jobnumber,
S.item AS customer
FROM
dbo.Project AS P
CROSS APPLY dbo.DelimitedSplit8K(P.code, ',') AS S





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64179 Visits: 17974
Just an fyi...the code you posted is what is known as a multi statement table valued function (mstvf). These are very poor performers. In fact, a typical scalar function or even a cursor will generally perform better.

I triple the suggestion to take a look at Jeff's splitter. It is fast!!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
waxb18
waxb18
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 111
Sean Lange (5/1/2014)
Just an fyi...the code you posted is what is known as a multi statement table valued function (mstvf). These are very poor performers. In fact, a typical scalar function or even a cursor will generally perform better.

I triple the suggestion to take a look at Jeff's splitter. It is fast!!!


Thanks for the suggestion and the advice.
I have taken a look at the splitter and it works very well.
However the purpose of this exercise is for my own development, nothing will be implemented as a business solution, I'm just trying to find the best way to get the outcome.

If i was to change the type of function to scalar, would this end up giving the correct dataset required?
I have also been looking at using a row_number function to define the data and then to iterate through each line, however i only return the final line (see below)


/****** Object: UserDefinedFunction [dbo].[SplitList] Script Date: 05/01/2014 16:51:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Function [dbo].[SplitCust3]
(

@CommaDelimiter [varchar](1)
)
RETURNS @Table Table (Value [varchar](100))


BEGIN

Declare @CustomerCode [varchar](max)
Declare @I INT


Select @I = ROW_NUMBER() OVER (Order by __CODE), @CustomerCode = [Customer Code]
From PROJECT
Where [Customer Code] like '%,%'
------------------------------------------------------------------------
------------------------------------------------------------------------


Set @I = 1
WHILE @I =1-- 10

BEGIN
DECLARE @sTemp [varchar](max)


SET @sTemp = ISNULL(@CustomerCode,'')
+ @CommaDelimiter
While LEN(@sTemp) > 0
Begin

INSERT INTO @Table
SELECT --@Project as Project,
SubString(@sTemp,1,
CharIndex(@CommaDelimiter,@sTemp)-1) as customer


SET @sTemp = RIGHT(@sTemp,
LEN(@sTemp)-CharIndex(@CommaDelimiter,@sTemp))
END


Set @I = @I+1

END

RETURN

END


Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1186 Visits: 653

DECLARE @tblNumber TABLE
(
ID INT
)

INSERT INTO @tblNumber
SELECT TOP 1000 ROW_NUMBER() OVER(ORDER BY S.Object_id) FROM sys.objects s,sys.objects si


DECLARE @tbl TABLE
(
ID INT, [Value] NVARCHAR(1000)
)

INSERT INTO @tbl
SELECT '13','Mitesh,Sohan,Oswal' UNION
SELECT '14','Pune,Kalyan,Maharashtra' UNION
SELECT '15','31,3110,311083' UNION
SELECT '16','311083,31,3110'

select t.ID,SUBSTRING(Value+',',tn.id,CHARINDEX(',',Value+',',tn.id)-tn.id)
from @tbl t INNER JOIN @tblNumber tn
ON LEN(t.[Value]) >=tn.id
where SUBSTRING(','+value,tn.id,1)= ','
Order by t.ID




Regards,
Mitesh OSwal
+918698619998
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50737 Visits: 10844
I heard you when you said this is a learning and growth exercise and not for production code. It seems likely that whatever you come up with will form a foundation for code that you do put into production...at least some day. This type of exercise has the ability to change your expectations of performance. I know the Tally table approach sure has changed mine.

There are, without a doubt, many ways to skin this cat. In addition to getting it to work, the very important point of performance should be one of your goals. If at all possible, my advice would be to approach this with the goal of making it an Inline Table Value Function (ITVF) for the sake of performance. The answer I would give (using DelimitedSplit8K) has already been posted, so I won't post anything further on that, but if you have the time, spend it trying the different ways to solve the problem. This process will help you grow and understand the performance implications of doing things differently.

If I could suggest one thing, it would be to test whatever you come up with on a 1M-row physical table. I find that it tends to expose weaknesses.

Have fun with the exercise!


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Jack Corbett
  Jack Corbett
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45725 Visits: 14925
waxb18 (5/2/2014)
[quote]
Thanks for the suggestion and the advice.
I have taken a look at the splitter and it works very well.
However the purpose of this exercise is for my own development, nothing will be implemented as a business solution, I'm just trying to find the best way to get the outcome.


I'm pretty confident that Jeff's DelimitedSplit8k is going to be the "best" way using T-SQL. "Best" being defined as the fastest way to return the correct results.


If i was to change the type of function to scalar, would this end up giving the correct dataset required?
I have also been looking at using a row_number function to define the data and then to iterate through each line, however i only return the final line (see below)


Changing the code to a scalar function would likely return the correct results, but do it very slowly as it will have to perform the loop through the delimited string for each row. It might give acceptable performance for 1000 rows, but wouldn't scale much beyond it. As Ed said, "There are, without a doubt, many ways to skin this cat", but Jeff's solution is the best way I've seen or used when implementing solely using T-SQL.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search