March 15, 2010 at 4:24 pm
Hi,
I have outline numbers such as I.A.1.b.
I am trying to split them into a string as follows I., I.A., I.A.1., I.A.1.b.
Here's what I have so far:
DECLARE @OutlineNumber VARCHAR(1000) = '.I.A.1.b'
SELECT SUBSTRING(@OutlineNumber,1,CHARINDEX('.',@OutlineNumber,N+1))
FROM dbo.Tally
WHERE N < LEN(@OutlineNumber)
AND SUBSTRING(@OutlineNumber,N,1) = '.'
March 15, 2010 at 5:42 pm
Something like this?
DECLARE @OutlineNumber VARCHAR(1000)
SET @OutlineNumber= '.I.A.1.b.'
SELECT STUFF(SUBSTRING(@OutlineNumber,1,CHARINDEX('.',@OutlineNumber,N+1)),1,1,'')
FROM dbo.Tally
WHERE N < LEN(@OutlineNumber)
AND SUBSTRING(@OutlineNumber,N,1) = '.'
The variable needs to have a dot at the beginning and at the then for this solution to work...
March 15, 2010 at 6:57 pm
You should use a funcation.
define a funcation like
CREATE FUNCTION [dbo].[SplittingByDot]
(
@SourceData varchar(255)
)
RETURNS NVARCHAR
AS
BEGIN
......
END
then use the
SELECT dbo.SplittingByDot(OutlineNumber) FROM dbo.Tally
get the result
March 15, 2010 at 8:58 pm
jarjarlee (3/15/2010)
You should use a funcation.define a funcation like
CREATE FUNCTION [dbo].[SplittingByDot]
(
@SourceData varchar(255)
)
RETURNS NVARCHAR
AS
BEGIN
......
END
then use the
SELECT dbo.SplittingByDot(OutlineNumber) FROM dbo.Tally
get the result
Other than writing a CLR, it would a lot faster to write an iTVF (Inline Table Valued Function) and then use CROSS APPLY to reference it.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 15, 2010 at 9:08 pm
If you're going to run this against sets of rows, consider writing it as an inline table-valued funcation.
CREATE FUNCTION dbo.tfn_DotSplitFuncation
(
@OutlineNumber VARCHAR(1000)
)
RETURNS TABLE
AS
RETURN
(
with cte (outlineNumber) as (select replace(replace('.^.','^',@outlineNumber),'..','.'))
SELECT STUFF(SUBSTRING(OutlineNumber,1,CHARINDEX('.',OutlineNumber,N+1)),1,1,'') AS OutlineNumber
FROM dbo.Tally
CROSS JOIN CTE
WHERE N < LEN(OutlineNumber)
AND SUBSTRING(OutlineNumber,N,1) = '.'
)
GO
/* test
-- get down, get funky
;with test (oln) as
(select 'I.A.2.g' union all
select 'IV.C.12,a' union all
select 'XIII.B.1,c,ii'
)
select *
from test
cross apply dbo.tfn_DotSplitFuncation(oln)
*/
Lutz, I stole your excellent code and just added a wrinkle to make starting and ending periods irrelevant.
Now pardon me, while I show my age....
"Awwww we want the func.... give up the func.... aww we need the func... gotta have that func!!"
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 15, 2010 at 9:09 pm
Jeff you were posting while I was funckifying.... 😛
Congrats on the 20k. They should create a Golden Pork Chop award and pass it on to future recipients like the Lombardi Trophy.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
March 16, 2010 at 12:43 am
Hi Goldie,
The following script is giving the outcome you want in my test database.
As you will see there is a string split function within the first part of the sql
Then a concetenation takes place in the second part.
You can find reference documents about the code for sql split string function and sql concatenation function
DECLARE @OutlineNumber VARCHAR(1000) = '.I.A.1.b';
with cte as (
select rn=ROW_NUMBER() over (order by id), val
from dbo.split(@OutlineNumber,'.')
where len(val ) > 0
)
select
STUFF
(
(
SELECT
'.' + c.val
FROM cte c
WHERE c.rn <= cte.rn
FOR XML PATH('')
), 1, 1, ''
) As concatenated_string
from cte
I hope that helps,
Eralper
March 16, 2010 at 7:04 am
Eralper (3/16/2010)
You can find reference documents about the code for sql split string function
Yikes. XML is not the right tool for the job when it comes to string splitting.
The tally-table method presented by Lutz, Bob, and Jeff is far, far, superior.
Almost as good a a CLR string splitting function in fact.
Comprehensive test results: http://florianreischl.blogspot.com/2009/09/high-performance-string-split-functions.html
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
March 16, 2010 at 10:19 am
lmu92 (3/15/2010)
Something like this?
DECLARE @OutlineNumber VARCHAR(1000)
SET @OutlineNumber= '.I.A.1.b.'
SELECT STUFF(SUBSTRING(@OutlineNumber,1,CHARINDEX('.',@OutlineNumber,N+1)),1,1,'')
FROM dbo.Tally
WHERE N < LEN(@OutlineNumber)
AND SUBSTRING(@OutlineNumber,N,1) = '.'
The variable needs to have a dot at the beginning and at the then for this solution to work...
This is exactly what I was looking for!
Thank you all for your help.
March 16, 2010 at 12:18 pm
Paul White (3/16/2010)
...Yikes. XML is not the right tool for the job when it comes to string splitting.
The tally-table method presented by Lutz, Bob, and Jeff is far, far, superior.
Almost as good a a CLR string splitting function in fact.
Comprehensive test results: http://florianreischl.blogspot.com/2009/09/high-performance-string-split-functions.html
Paul
I have to object!
Not because of the method used but because of giving me credit for it in this case.
Actually, Goldie already used the Tally solution. I just added the STUFF() part, which has been further improved by "Dixie-Bob".
So, Dixie, you haven't stolen any kind of excellent code as far as I'm concerned. Credit belongs to Goldie.
March 16, 2010 at 7:13 pm
Paul White (3/16/2010)
The tally-table method ... {snip} ... is far, far, superior.Almost as good a a CLR string splitting function in fact.
Heh... keepsake that is right thar...
--Jeff Moden
Change is inevitable... Change for the better is not.
March 16, 2010 at 9:34 pm
lmu92 (3/16/2010)
I have to object! Not because of the method used but because of giving me credit for it in this case.Actually, Goldie already used the Tally solution. I just added the STUFF() part, which has been further improved by "Dixie-Bob".
So, Dixie, you haven't stolen any kind of excellent code as far as I'm concerned. Credit belongs to Goldie.
Ha, OK! 🙂
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
March 16, 2010 at 9:35 pm
Jeff Moden (3/16/2010)
Paul White (3/16/2010)
The tally-table method ... {snip} ... is far, far, superior.Almost as good a a CLR string splitting function in fact.
Heh... keepsake that is right thar...
:laugh: Well, it is true!
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply