Split a string at the first space after 50 characters

  • Hi All,

    I have long descriptive varchar(max) column

    declare @STR varchar(max)

    set @STR = 'Guidelines and recommendations for lubricating the required components of your Adventure Works Cycles bicycle. Component lubrication is vital to ensuring a smooth and safe ride and should be part of your standard maintenance routine. Details instructions are provided for each bicycle component requiring regular lubrication including the frequency at which oil or grease should be applied'

    I want to split the long string into multiple rows at the first space after 50 characters so the word is not split between with the sequence no. i also dont want to trim any spaces in the string while splitting which is caused by len function.

    so the out put required should be like as follows

    'Guidelines and recommendations for lubricating the' 1

    ' required components of your Adventure Works Cycles ' 2

    ' bicycle. Component lubrication is vital to ensuring' 3

    and so on...

    Would appreciate any help.

    Thanks

    Sud

  • take a look at this thread, where there's a couple of different ways the same problem was tackled:

    Large String Value needs to be broken down into Human readible fixed lengths.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (11/13/2013)


    take a look at this thread, where there's a couple of different ways the same problem was tackled:

    Large String Value needs to be broken down into Human readible fixed lengths.

    Lowell - What an outstanding thread to pass on as it seems to exactly match the OP's problem.

    My question to you sir. How did you manage to dig up that fossil of a post? Were you perhaps an archeology student in the past?

    I have never been able to get the SSC search function to work well when looking for past posts.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (11/13/2013)


    Lowell (11/13/2013)


    take a look at this thread, where there's a couple of different ways the same problem was tackled:

    Large String Value needs to be broken down into Human readible fixed lengths.

    Lowell - What an outstanding thread to pass on as it seems to exactly match the OP's problem.

    My question to you sir. How did you manage to dig up that fossil of a post? Were you perhaps an archeology student in the past?

    I have never been able to get the SSC search function to work well when looking for past posts.

    Dwain ive got a pretty good memory for posts i read, especially if i tested the code myself. I try to save a link to the original post in my snippets with the code example( i had this saved), normally would have search for a uniquish string from the code i saved but in this case i searched for "wordwrap " and found it right awayd

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Guys That helps.

    Sud

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply