Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query Based on a String Range

Author  Topic 

RaiderRed
Starting Member

2 Posts

Posted - 2003-04-08 : 09:49:45
Hello,

Does anyone know how (in SQL 2000) to create a query that returns records based on a range of characters for the given first 2 letters? For example, if I'm querying a table of employees and I want all the employees whose last names begin with "Sa" through "Th", how would I do this?

I know how to do it if it's just one letter specified. For example, if it was just S - T, I would code it like this:

select lastname
from employee
where lastname like '[S-T]%'
order by lastname

But this doesn't work if I specify more than one character in my range. Also, the BETWEEN doesn't work because the range is dynamic based on user input. So I need the query to be INCLUSIVE of the range specified.

Any help would be greatly appreciated....

Thanks.

X002548
Not Just a Number

15586 Posts

Posted - 2003-04-08 : 09:53:08
How about:

SUBSTRING(Col1,1,2) BETWEEN @locvar1 AND @locvar2

Where the local variables are 2 position chars...

what'dya think?



Brett

8-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-04-08 : 09:54:33
Try this:

select lastname
from employee
where lastname between @FirstLetters AND @LastLetters + 'Z'
order by lastname

That should work fine; replace the 'Z' with another character with a higher ASCII value as necessary.

- Jeff
Go to Top of Page

RaiderRed
Starting Member

2 Posts

Posted - 2003-04-08 : 10:08:08
Thanks, Jeff! I tried your suggestion and it works great -- I appreciate your feedback.



Go to Top of Page
   

- Advertisement -