Selecting range based on criteria

  • Greetings all, I need some help with a T-SQL statement which will return a range of rows based off a varchar field. My stored proc will pass in a begin and end text criteria and I must pass back the rows in between. I'm thinking I would use the LIKE construct but not exactly sure how. Can someone get me started?

    Thanks in advance.


    Regards,
    Bob Szymanski
    Blue Mule Software

  • Is this what you are looking for?

    declare @Temp  table (pk int, v varchar(20))

    insert into @temp

    select 1, 'joe'

    union all

    select 2, 'jim'

    union all

    select 3, 'betty'

    union all

    select 4, 'zack'

    union all

    select 5, 'tim'

    union all

    select 6, 'johanna'

    union all

    select 7, 'robbin'

    union all

    select 8, 'joey'

    select *

    from @temp

    where v between 'joe' and 'tim'

  • Hi Adrienne, thanks for your reply. Yes, you are real close. One thing I need though is the case when either criteria be partial. For example, modifying your sample above:

    declare @Temp table (pk int, v varchar(20))

    insert into @temp

    select 1, 'joe'

    union all

    select 2, 'jim'

    union all

    select 3, 'betty'

    union all

    select 4, 'zack'

    union all

    select 5, 'tim'

    union all

    select 6, 'johanna'

    union all

    select 7, 'robbin'

    union all

    select 8, 'joey'

    select *

    from @temp

    where v between 'joe' and 'ti'

    Yields the following:

    1joe

    6johanna

    7robbin

    8joey

    ** What I need is to include tim as well.

    Thanks.


    Regards,
    Bob Szymanski
    Blue Mule Software

  • How about...

    select *

    from @temp

    where v between 'joe' and 'ti'

    or v like 'joe%'

    or v like 'ti%'

     

  • Yep, that'll work.

    Thanks for you help Adrienne, I appreciate it.


    Regards,
    Bob Szymanski
    Blue Mule Software

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

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