August 8, 2007 at 10:07 am
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.
August 8, 2007 at 10:42 am
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'
August 8, 2007 at 12:49 pm
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.
August 8, 2007 at 1:24 pm
How about...
select *
from @temp
where v between 'joe' and 'ti'
or v like 'joe%'
or v like 'ti%'
August 8, 2007 at 3:13 pm
Yep, that'll work.
Thanks for you help Adrienne, I appreciate it.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply