Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Splitting the contents of a column

Author  Topic 

CJ81
Starting Member

9 Posts

Posted - 2010-08-26 : 06:59:21
Hello all,

I have a table which contains a column called "PropertyValue". This value is an amalgamation of up to three pieces of data entered by the user. The three pieces are data are separated by a  symbol.

Here are some examples:

PriceServiceRelationship (three pieces of data entered)
PriceRelationship (two pieces of data entered)
Service (one piece of data entered)

The problem is that I need to split the data out, so that I have separate records for each, or at the very least replace the  symbol with something more meaningful, such as a comma.

I have tried using LEFT and CHARINDEX to search for the  symbol but the user can select up to three items so I don't know how many  symbols will be in the string.

I am totally lost ... can anyone point me in the right direction?

Thanks!
CJ81

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-26 : 07:24:04
See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

CJ81
Starting Member

9 Posts

Posted - 2010-08-26 : 07:31:46
Thanks Peso. I think from reading the linked post that you are recommending using a function. Is it not possible to code this in SQL directly in the query pane?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-26 : 08:18:20
Yes you can.
DECLARE	@Sample TABLE
(
ID INT NOT NULL,
Data VARCHAR(MAX) NOT NULL
)

INSERT @Sample
SELECT 3, 'PriceServiceRelationship' UNION ALL
SELECT 2, 'PriceService' UNION ALL
SELECT 1, 'Price' UNION ALL
SELECT 0, ''

-- Peso 1
SELECT s.ID,
SUBSTRING(s.Data, p1.FromPos, 1 + p1.ToPos - p1.FromPos) AS Option1,
SUBSTRING(s.Data, p2.FromPos, 1 + p2.ToPos - p2.FromPos) AS Option2,
SUBSTRING(s.Data, p3.FromPos, 1 + p3.ToPos - p3.FromPos) AS Option3
FROM @Sample AS s
OUTER APPLY (
SELECT 1,
CHARINDEX('', s.Data + '', 1) - 1
WHERE LEN(s.Data) > 0
) AS p1(FromPos, ToPos)
OUTER APPLY (
SELECT p1.ToPos + 2,
CHARINDEX('', s.Data + '', p1.ToPos + 2) - 1
WHERE LEN(s.Data) > p1.ToPos
) AS p2(FromPos, ToPos)
OUTER APPLY (
SELECT p2.ToPos + 2,
CHARINDEX('', s.Data + '', p2.ToPos + 2) - 1
WHERE LEN(s.Data) > p2.ToPos
) AS p3(FromPos, ToPos)

-- Peso 2
SELECT ID,
NULLIF(xmlData.value('x[1]', 'VARCHAR(MAX)'), '') AS Option1,
xmlData.value('x[2]', 'VARCHAR(MAX)') AS Option2,
xmlData.value('x[3]', 'VARCHAR(MAX)') AS Option3
FROM (
SELECT ID,
CAST('<x>' + REPLACE(Data, '', '</x><x>') + '</x>' AS XML) AS xmlData
FROM @Sample
) AS d


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

CJ81
Starting Member

9 Posts

Posted - 2010-08-26 : 08:59:20
Truly an expert - thanks Peso! This works great - I should be able to adapt it to suit. Thanks very much!
Go to Top of Page
   

- Advertisement -