SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


parse/split string


parse/split string

Author
Message
Rich96
Rich96
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 236
I have 2 separate tables 1 normalized and the other not. It does however have 1 column that contains the data I need to join to the normalized table. The column is 1 long string concatenated with a '~'. I am trying to split the string into parts but am having trouble figuring this one out. The first part is a fixed length but the second part is not a fixed length.

I can get the first part (in bold) of the string but not sure how to grab the second part (bold).
This is how I'm doing that:
left(substring(docid,charindex('~',docid)+1,len(docid)),7)


Table snippet:
99~B007340~9822151~LR5~250
99~B007599~9417974~LC0~240


Thanks for taking a look.
Rich96
Rich96
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 236
I finally hit my search correctly... Jeff Moden already posted an amazingly simple solution!

http://www.sqlservercentral.com/Forums/FindPost452711.aspx


Thread closed....
Mike01
Mike01
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2001 Visits: 1533
Are the format of the records in this table all the same? try these


declare @vt_t table (docid varchar(50))

insert into @vt_t
select '99~B007340~9822151~LR5~250' union all
select '99~B007599~9417974~LC0~240'

select substring(docid,charindex('~',docid)+1,7)FirstString,
substring(docid,CharIndex('~', docid, charindex('~',docid)+1 + 1) + 1, 7) SecondString
from @vt_t

-- Use this if the format for all records are the same
select substring(docid, 4, 7) FirstString,
substring(docid, 12,7) SecondString
from @vt_t



For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Rich96
Rich96
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 236
Mike.. the parts are not fixed length.
Your first select is returning great results but the occasional '~' is on the end of some records.

B005739 997706~

I added a replace on the SecondString.

select substring(docid,charindex('~',docid)+1,7)FirstString,
replace(substring(docid,CharIndex('~', docid, charindex('~',docid)+1 + 1) + 1, 7),'~','') SecondString
from mytable
Rich96
Rich96
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 236
Mike-I just realized the query is substring the first 7 characters of the SecondString. That string will be variable in length.
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