trimming a string

  • How would you trim a string that looks like this? I need only the string between the first set of backslashes.

    \Beverages\Soda Pop\Mountain Dew

    -

  • xenophilia (4/11/2012)


    How would you trim a string that looks like this? I need only the string between the first set of backslashes.

    \Beverages\Soda Pop\Mountain Dew

    Is the first backslash always in position 1 of the string?

  • yes (thanks again Lynn)

    -

  • See if this helps you:

    DECLARE @tststr VARCHAR(64);

    SET @tststr = '\Beverages\Soda Pop\Mountain Dew ';

    SELECT SUBSTRING(RIGHT(@tststr,DATALENGTH(@tststr) - 1),1,PATINDEX('%\%',RIGHT(@tststr,DATALENGTH(@tststr) - 1)) - 1);

  • can i substitute the column name for the @parameter?

    eg.

    ,SUBSTRING(RIGHT('DTS.SuitePath',DATALENGTH('DTS.SuitePath') - 1),1,PATINDEX('%\%',RIGHT('DTS.SuitePath',DATALENGTH('DTS.SuitePath') - 1)) - 1)

    as 'SuitePath'

    -

  • BTW, the string between the first pair of backslashes will vary in length from row to row

    -

  • Yes, but not in quotes.

    'DTS.SuitePath' means the literal string value DTS.SuitePath. If it's a column name, it must not be in quotes.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Instead of constantly asking questions how about trying things out yourself?

  • Hello Gail!

    So, that results in empty cells. I know I should post some ddl, but any ideas?

    -

  • xenophilia (4/11/2012)


    BTW, the string between the first pair of backslashes will vary in length from row to row

    And your point? The only fixed point the first \ in the string. I'm not looking for that per your specifications.

  • Are we talking T-SQL code or SSIS?

  • xenophilia (4/11/2012)


    Hello Gail!

    So, that results in empty cells. I know I should post some ddl, but any ideas?

    so why dont you post what you know we will ask for? it will get you better answers to your questions.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • ok, because I'm stressed 🙂 I will.

    -

  • Here's the query. I am using the column name in place of the parameter. It is a working query before I add the trimming functions (line 2). I know Lynn's select string works great, but when I adopt using column name it fails in my query.

    ------i get this error

    Msg 536, Level 16, State 1, Line 1

    Invalid length parameter passed to the substring function.

    ---query

    removed

    -

  • Table definitions and sample data please (sample data as insert statements)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 15 posts - 1 through 15 (of 32 total)

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