October 25, 2009 at 5:10 am
I have a table
CREATE TABLE #IMaster (ID INT, ITEM1 VARCHAR(100))
GO
INSERT #IMaster
SELECT 1,'pencil'
UNION
SELECT 2,'pen/pencil'
UNION
SELECT 3,'ink'
UNION
SELECT 4,'pen/pencil/ink/'
I need to create a view like this
IDid1 ITEM1
11pencil
21pen
22pencil
31ink
41pen
42pencil
43ink
We are still working on sql 2000.
Thanks in advance
October 25, 2009 at 2:34 pm
check SSC for a split (table valued) function .
e.g.
/*
* Split a delimited string using a table valued function with XML
*/
-- table-valued function
CREATE FUNCTION dbo.fn_DBA_Split2(@Data varchar(4000),
@Delimiter varchar(10) = ',')
RETURNS @tblSplit TABLE
(ItemId int IDENTITY(1, 1) NOT NULL PRIMARY KEY,
Item varchar(4000) NULL)
AS
BEGIN
-- select e.*
-- from pubs..employee E
-- inner join master.dbo.fn_DBA_Split2('ikke,jij,hij,zij,Karin',',') U
-- on E.fname = U.item
DECLARE @x XML
SET @x = '<i>' + REPLACE( @data, @Delimiter, '</i><i>') + '</i>'
INSERT INTO @tblSplit (Item)
SELECT x.i.value('.', 'VARCHAR(4000)')
FROM @x.nodes('//i') x(i)
RETURN
END
go
select e.*
from pubs..employee E
inner join master.dbo.fn_DBA_Split2('ikke,jij,hij,zij,Karin',',') U
on E.fname = U.item
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
October 25, 2009 at 10:00 pm
Oh... be careful, Johan... XML splits are relatively slow compared to many other methods.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 25, 2009 at 10:02 pm
Jay_dba (10/25/2009)
I have a table
CREATE TABLE #IMaster (ID INT, ITEM1 VARCHAR(100))
GO
INSERT #IMaster
SELECT 1,'pencil'
UNION
SELECT 2,'pen/pencil'
UNION
SELECT 3,'ink'
UNION
SELECT 4,'pen/pencil/ink/'
I need to create a view like this
IDid1 ITEM1
11pencil
21pen
22pencil
31ink
41pen
42pencil
43ink
We are still working on sql 2000.
Thanks in advance
Take a look at the following article, Jay.
http://www.sqlservercentral.com/articles/T-SQL/62867/
--Jeff Moden
Change is inevitable... Change for the better is not.
October 26, 2009 at 1:56 am
Thank you ALZDBA, Jeff .
ALZDBA,
I am getting an error on @x.nodes('/i') x(i) . can you please check.
Jeff,
Thanks for the article. its too good.
But my problem still exists
October 26, 2009 at 2:03 am
Jeff Moden (10/25/2009)
Oh... be careful, Johan... XML splits are relatively slow compared to many other methods.
Indeed, I copy/pasted the wrong example code out of one of my test scripts.
That's why I hinted to search ssc for split functions.
OP would have come to your marvellous article without a doubt 😉
Should have pointed to that in the first place 😎
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
October 26, 2009 at 2:09 am
Jeff Moden (10/25/2009)
Oh... be careful, Johan... XML splits are relatively slow compared to many other methods.
Plus the OP indicated he was still on SQL 2000 (despite posting in the SQL 2005 forums). So XML is out of the picture anyway.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 26, 2009 at 2:15 am
Jay_dba (10/26/2009)
Thank you ALZDBA, Jeff .ALZDBA,
I am getting an error on @x.nodes('/i') x(i) . can you please check.
Jeff,
Thanks for the article. its too good.
But my problem still exists
Aaarchh ... sql2000 ..... doesn't have XML datatype.
My bad. Sorry for that.
You'll get there using Jeffs article !
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply