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 2008 Forums
 Transact-SQL (2008)
 PARSENAME/REPLACE or Help with Split

Author  Topic 

mimuk
Starting Member

19 Posts

Posted - 2014-07-21 : 06:57:26
I have a MySQL Linked Server in my MS SQL Environment
I have created a SQL View to one of the MySQL tables as follows

SELECT id, contract, eventuser, eventtype, eventtext, CONVERT(varchar, eventdt, 100) AS dateactioned
FROM DATABASE...contractsaudit

The event text column contains the following data e.g

P2:2:Y:Kitchen
P3:1:N:Main Office

I need to split the data into multiple values (always 4 columns in that order) i.e
P3:1:N:Main Office would be

Product | Amount | Swapped | Location
P3 | 1 | N | Main Office
P2 | 2 | Y | Kitchen

Please can someone help?

Many thanks,

Mim

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-07-21 : 08:09:51
Look up documentation on charindex and substring
Go to Top of Page

mimuk
Starting Member

19 Posts

Posted - 2014-07-21 : 10:54:22
Thanks gbritton
I ended up using parsename(replace)
PARSENAME(REPLACE(eventtext, ':', '.'), 1) AS product, PARSENAME(REPLACE(eventtext, ':', '.'), 2) AS Adjustment, PARSENAME(REPLACE(eventtext, ':', '.'), 3) AS Swapped etc.
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-07-21 : 11:26:47
good call!
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2014-07-21 : 13:14:13
Be aware that you will get an error if a '.' ever appears in your data. Or the first time there is a fifth column of data for any reason.

Go to Top of Page
   

- Advertisement -