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


Split a String


Split a String

Author
Message
AndrewSQLDBA
AndrewSQLDBA
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2236 Visits: 3427
Hello Everyone
I am working on some old data that should not be store this way, but it is.

My data is two values with pipe separators.

10.0||14.5
or
2||34
or
7.1||19
or
4||11.7



I need to query this column and get each value separately. I have no idea how to go about this. There are always double pipe in the middle. But as you can see, there may or may not be decimal values in each. I think that is what is throwing me off. When perhaps, it really may not matter about the actual values.

I appreciate any and all assistance, suggestions and comments

Andrew SQLDBA
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6312 Visits: 5478
http://www.sqlservercentral.com/articles/Tally+Table/72993/
by Jeff Moden

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
AndrewSQLDBA
AndrewSQLDBA
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2236 Visits: 3427
I was actually looking for something else. That really did not help.

Andrew SQLDBA
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21500 Visits: 19689
AndrewSQLDBA (3/12/2013)
I was actually looking for something else. That really did not help.

Andrew SQLDBA


What's the problem with it, Andrew? There is an alternative; I'm just curious.

“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
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6312 Visits: 5478
AndrewSQLDBA (3/12/2013)
I was actually looking for something else. That really did not help.

Andrew SQLDBA


Ok, then could you please provide a bit more details about what you have and expected you really want. From the way you have described your problem in the first post it did look like you were after common string split function.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6312 Visits: 5478
Let me one guess (just a specualtion). Do you want values split to columns? If so, just use the udf like that:


SELECT *
INTO #test
FROM (VALUES ('10.0||14.5'), ('2||34'), ('7.1||19'), ('4||11.7') ) v(val)
;

SELECT val, v1.Item, v2.Item
FROM #test
CROSS APPLY (SELECT Item FROM dbo.DelimitedSplit8k(val,'|') WHERE ItemNumber = 1) v1
CROSS APPLY (SELECT Item FROM dbo.DelimitedSplit8k(val,'|') WHERE ItemNumber = 3) v2



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
AndrewSQLDBA
AndrewSQLDBA
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2236 Visits: 3427
For one thing, I do not have a Tally table setup on the server in which I am working.

So, are there any other ways of doing this without a tally table. There are only about 100,000 rows, and this is a one time update. So I do not really care about performance. What I am most concerned with is the amount of time to have this completed.

Yes, I can select the data into a temp table, in separate columns there. That would be fine. I can perform the update and then do what I need.

Thanks in advance
Andrew SQLDBA
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50850 Visits: 38662
AndrewSQLDBA (3/12/2013)
For one thing, I do not have a Tally table setup on the server in which I am working.

So, are there any other ways of doing this without a tally table. There are only about 100,000 rows, and this is a one time update. So I do not really care about performance. What I am most concerned with is the amount of time to have this completed.

Yes, I can select the data into a temp table, in separate columns there. That would be fine. I can perform the update and then do what I need.

Thanks in advance
Andrew SQLDBA


Actually, you don't need a tally table to run the DelimitedSplit8K function. It uses a dynamic tally table built in the function.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
mister.magoo
mister.magoo
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5394 Visits: 7871
AndrewSQLDBA (3/12/2013)
Hello Everyone
I am working on some old data that should not be store this way, but it is.

My data is two values with pipe separators.

10.0||14.5
or
2||34
or
7.1||19
or
4||11.7



I need to query this column and get each value separately. I have no idea how to go about this. There are always double pipe in the middle. But as you can see, there may or may not be decimal values in each. I think that is what is throwing me off. When perhaps, it really may not matter about the actual values.

I appreciate any and all assistance, suggestions and comments

Andrew SQLDBA


For a one-off update, just use charindex

SELECT LEFT(value,CHARINDEX('||',value)-1) as Part1,STUFF(value,1,CHARINDEX('||',value)+1,'') as Part2



MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • AndrewSQLDBA
    AndrewSQLDBA
    SSCrazy
    SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

    Group: General Forum Members
    Points: 2236 Visits: 3427
    Thank You Mister Magoo
    That worked perfectly

    Thanks for the suggestions everyone, just way overkill for what was actually needed.

    Andrew SQLDBA
    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