Home > Archive > Microsoft SQL Server forum > January 2006 > splitting a comma seperated column into multiple rows









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author splitting a comma seperated column into multiple rows
Hennie7863

2006-01-18, 9:23 am

Hi i want to create a table as follows :

if exists (select * from dbo.sysobjects where id =
object_id(N'[Indexes]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Indexes]
GO

Create table Indexes(indexname Varchar(100), index_Description
Varchar(100), index_keys Varchar(100))
GO

INSERT INTO Indexes EXEC sp_helpindex 'SDM_Fact_Order_Deta
il'
GO

This will give me a table (northwind)

IX_Auto_SDM_Fact_FK_
Shipped_Date nonclustered located on
SAMIS_SDM_Index FK_Shipped_Date

IX_Auto_SDM_Fact_Ord
er_Detail_FK_Insert_
Date clustered located on
SAMIS_SDM_Data1 FK_I
nsert_Date, FK_Insert_Time

As you see sp_helpindex will give me a comma seperated field. I want
to split the third column FK_Insert_Date, FK_Insert_Time into a extra
row
Like this :

IX_Auto_SDM_Fact_FK_
Shipped_Date FK_Shipped_Date
IX_Auto_SDM_Fact_Ord
er_Detail_FK_Insert_
Date FK_Insert_Date
IX_Auto_SDM_Fact_Ord
er_Detail_FK_Insert_
Date FK_Insert_Time

Can anyone help me with this?

Thanx

Hennie

Razvan Socol

2006-01-18, 9:23 am

Hi, Hennie

If you need the columns that are part of an index for a specified
table, you can use something like this:

SELECT i.name as IndexName, c.name as ColumnName
FROM sysindexes i
INNER JOIN sysindexkeys k ON i.id=k.id AND i.indid=k.indid
INNER JOIN syscolumns c ON c.id=k.id and c.colid=k.colid
WHERE i.id=OBJECT_ID('Order Details')

If you really want to split some other comma-delimited value (that
cannot be obtained in any other way), see this article by Erland
Sommarskog, SQL Server MVP:
http://www.sommarskog.se/arrays-in-sql.html

Razvan

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com