Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Splitting a string into fields


Splitting a string into fields

Author
Message
Douglasjbell
Douglasjbell
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 70
Hi All

I have a field called Defects which contain strings like the following. The string makeup is always the same.
Scrap : Part Assembly : Surface Defects : Scratch
Scrap : Part Assembly : Components : Wrong Components
Scrap : Part Assembly : Other : Change Over
Repair : Punching : Surface Damages : Crack

What I would like to do is create a view that splits the string by the : and have them displayed as individual fields.

I started of using CharIndex to find the : and then use left and the charindex to strip the first part out, but I felt doing it this way would involve a lot of LEFTs and RIGHTs to split up the whole string, is there an easier way to accomplish this?

Cheers

DJ
Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
Take a look at this article by Jeff Moden.

http://www.sqlservercentral.com/articles/Tally+Table/72993/
Douglasjbell
Douglasjbell
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 70
Hi

Thanks for the reply, that whole post is a little above my head, but I will give it another read to see if I can make sense out of it.

Cheers

Dj
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8973 Visits: 19025
If there really are always four elements, then you could use PARSENAME (look it up in BOL):

;WITH SampleData AS (
SELECT MyString = 'Scrap : Part Assembly : Surface Defects : Scratch' UNION ALL
SELECT 'Scrap : Part Assembly : Components : Wrong Components' UNION ALL
SELECT 'Scrap : Part Assembly : Other : Change Over' UNION ALL
SELECT 'Repair : Punching : Surface Damages : Crack')

SELECT
MyString,
Element1 = PARSENAME(REPLACE(MyString,' : ','.'),4),
Element2 = PARSENAME(REPLACE(MyString,' : ','.'),3),
Element3 = PARSENAME(REPLACE(MyString,' : ','.'),2),
Element4 = PARSENAME(REPLACE(MyString,' : ','.'),1)
FROM SampleData



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
That Parsename always sneaks up on me..
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search