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)
 SubString( Hell)

Author  Topic 

pinkfloyd43
Starting Member

5 Posts

Posted - 2009-02-03 : 13:31:05
I have a column ActivityDescription which contains the literl value below:


From: Account: DEMO Dictator: Doctor, Demo To: Account: EMC Dictator: Clark, Nellda



I have been trying to extract the data bolded for too long. Anybody have a easy solution for this, SubString() and PatIndex() have been tried but can't seem to get it exactly correct!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-03 : 13:33:54
quote:
Originally posted by pinkfloyd43

I have a column ActivityDescription which contains the literl value below:


From: Account: DEMO Dictator: Doctor, Demo To: Account: EMC Dictator: Clark, Nellda



I have been trying to extract the data bolded for too long. Anybody have a easy solution for this, SubString() and PatIndex() have been tried but can't seem to get it exactly correct!



How do you wanna extract? In CSV.
Go to Top of Page

pinkfloyd43
Starting Member

5 Posts

Posted - 2009-02-03 : 13:44:11

Was trying from SQL Statement

Select bla,
SubString())))) As 'From Org'
SubString())))) As 'From Dict'
etc, etc.....................
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 13:47:37
try this (untested)

SELECT SUBSTRING (Col,PATINDEX('%From: Account:%',Col)+15,PATINDEX('%Dictator:%',Col)-PATINDEX('%From: Account:%',Col)-16) AS FromAcct,
SUBSTRING (Col,PATINDEX('%Dictator:%',Col)+10,PATINDEX('%To: Account:%',Col)-PATINDEX('%Dictator:%',Col)-11) AS FromDictator,
REVERSE(SUBSTRING(REVERSE(Col),PATINDEX('%:rotatciD%',REVERSE(Col))+10,PATINDEX('%:tnuoccA :oT%',REVERSE(Col))-PATINDEX('%:rotatciD%',REVERSE(Col))-11)) AS ToAcct,
REVERSE(LEFT (REVERSE(Col),PATINDEX('%:rotatciD%',REVERSE(Col))-1)) AS ToDictator
FROM Table
Go to Top of Page

pinkfloyd43
Starting Member

5 Posts

Posted - 2009-02-03 : 14:00:56
This is somewhat what I was coming up with but it needs to be dynamic in nature, I was showing one row, lengths of data will vary by row and as such must be dyanmic in solution, your response was quite a bit longer than where I had gotten!

Funny when someone puts data like this into a column and are saying don't worry we will never need to report on it!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-03 : 15:40:43
quote:
Originally posted by pinkfloyd43



Funny when someone puts data like this into a column and are saying don't worry we will never need to report on it!




You should recommend to them to normalize this data then. Not only will it be easier to query, but it should also be faster to query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -