Search string based on length of parameter and start of path

  • Could some one help with my search string. I have apth like 1-1-1 and there could be 10-20-1 and i only want to select the first character based on the parameter length e.g if i pass in 10 it will only select the Path_String 10 that begin with 10 or it was 1 it would select the Path_string Starting at 1

    SELECT ID, AddedBy, Title, Path_String FROM MyTable WHERE Path_String Like '%' + @Paramter + '%'; this isn't what I want.

  • SELECT ID, AddedBy, Title, Path_String

    FROM MyTable

    WHERE

    Path_String Like @Paramter + '%'

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • That still selects 1, 12, 13 etc if the parameter is just 1

  • kirkdm01 (1/25/2012)


    Will that select 1 and 12 where the parameter could just be 1 because i just would like it to select 1

    OOPS!

    SELECT ID, AddedBy, Title, Path_String

    FROM MyTable

    WHERE

    Path_String Like @Paramter + '-%'

    Oh, and read the article referenced in my signature, Sample data, DDL and desired output will help. 🙂

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • I tried you're second approach and i get no results if i put in 13, 12 or 1

  • kirkdm01 (1/25/2012)


    I tried you're second approach and i get no results if i put in 13, 12 or 1

    Read the article referenced in my signature please.

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • kirkdm01 (1/25/2012)


    I tried you're second approach and i get no results if i put in 13, 12 or 1

    If you spell "Parameter" correctly :-P, Jason's code seems to do the trick...

    DECLARE @MyTable TABLE (Path_String VARCHAR(100));

    INSERT INTO @MyTable (Path_String)

    SELECT '1-9-8' UNION ALL

    SELECT '12-1-2' UNION ALL

    SELECT '13-3-4' UNION ALL

    SELECT '15-5-6'

    ;

    DECLARE @Parameter VARCHAR(100);

    SELECT @Parameter = '1';

    SELECT Path_String

    FROM @MyTable

    WHERE

    Path_String Like @Parameter + '-%'

    ;

    --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)

  • It ended being the hyphen character being the problem so i use '/%' instead and it worked

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

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