string question

  • Hi,

    I have a column with a string longer than life. 🙁

    I need to take a specific part of it, I know the is the start and where is the end.

    The problem is that each string has a different size, so I cannot determinate the length :w00t:

    so every time i try to use the substring, i get different results cause i am not calculating the correct size.

    for example I have the following text.

    Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.

    I would like to take all the text since yesterday till tomorrow. but the size of the string might change in each row.

    :blush: help please

    thanks

  • When eyeballing the data, how do you know where the start and end are? Is there any logic that can be applied to every single row? E.g everything after the second space, everything after the word 'yesterday'.

    Also, you can determine the length using LEN(colname) if that's all you're missing.

  • Maybe something like this:

    SELECT String,

    SUBSTRING( String, CHARINDEX('yesterday', String), CHARINDEX('tomorrow', String) - CHARINDEX('yesterday', String) + 8 /*LEN('tomorrow')*/)

    FROM(VALUES('Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.'))x(String);

    As tindog mentioned, the important thing is to know how will you delimit the start and length of the string.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • thanks!!!! :kiss:

    it worked

  • astrid 69000 (2/25/2016)


    thanks!!!! :kiss:

    it worked

    The question now is... do you understand it well enough to both maintain it and do it again for something else?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • yes, i do, cause it was similar to what i had and i was missing what length to count.

    i do check 😉

    thanks again 🙂

  • astrid 69000 (2/25/2016)


    yes, i do, cause it was similar to what i had and i was missing what length to count.

    i do check 😉

    thanks again 🙂

    You bet and thank you for the feedback. From the "other" recent post of yours, you really seem to be interested in T-SQL and just wanted to make sure you understood.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

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