SQLTeam.com | Weblogs | Forums

Query help


#1

I have a following string

test = '/one column/second_column/thirdcolumn/fournthcolumn'

i need a query to extract from above string to produce the below output

test = /one column/second_column

Thanks for your help in advance


#2

Use a string splitter function like shown below. In the example here I am using dbo.DelimitedSplit8K which is available here

DECLARE @test VARCHAR(256) = '/one column/second_column/thirdcolumn/fournthcolumn';
SELECT STUFF((
SELECT
	'/' + [Item]
FROM
(
	SELECT * FROM dbo.DelimitedSplit8K(@test,'/')
	WHERE ItemNumber <= 3
)s FOR XML PATH('')), 1,1,'');