Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

In-Line Function to split string Expand / Collapse
Author
Message
Posted Thursday, May 1, 2014 10:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 31, 2014 4:08 AM
Points: 22, Visits: 65
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
Post #1566784
Posted Thursday, May 1, 2014 10:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 31, 2014 4:08 AM
Points: 22, Visits: 65
Thinking about it now maybe a SPROC would have been better to use
Post #1566786
Posted Thursday, May 1, 2014 11:01 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Sunday, October 12, 2014 4:00 PM
Points: 341, Visits: 751
Why not use this splitter?

http://www.sqlservercentral.com/articles/Tally+Table/72993/
Post #1566793
Posted Thursday, May 1, 2014 11:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, November 21, 2014 1:42 PM
Points: 10,340, Visits: 13,341
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

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
Post #1566797
Posted Thursday, May 1, 2014 12:09 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 6:31 PM
Points: 13,207, Visits: 12,688
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 Moden's 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)
Post #1566817
Posted Friday, May 2, 2014 4:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 31, 2014 4:08 AM
Points: 22, Visits: 65
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

Post #1566978
Posted Friday, May 2, 2014 4:53 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, August 29, 2014 11:34 PM
Points: 790, Visits: 646
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
Post #1566984
Posted Friday, May 2, 2014 6:07 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 5:27 AM
Points: 4,456, Visits: 3,907
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
Post #1567002
Posted Friday, May 2, 2014 6:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, November 21, 2014 1:42 PM
Points: 10,340, Visits: 13,341
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

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
Post #1567004
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse