approximate vlookup in sql

How does one do a function in sql like approximate vookup(end is true and not false). do you still use some type of join for this?

sql '05
LVL 1
k1ng87Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tigin44Commented:
can you explain what you want more?
0
sachinpatil10dCommented:
yes we will have to use join
0
k1ng87Author Commented:
Range | Value
------------------
1-4           a
5-8           b
11-15       c

If I had another table with a value 3 and I joined it on column Range, I should get back value A from the value column. If i was joining on a value that was 9, I should get back since 9 falls out in any ranges.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

k1ng87Author Commented:
i should get back a null for a value of 9
0
Scott PletcherSenior DBACommented:
Yes, it's a join, but it needs to be LEFT join, since you may not find a match:

SELECT ..., vt.value, ...
FROM main_table mt
...other join(s)...
LEFT OUTER JOIN vlookup_table vt ON
    mt.search_value BETWEEN vt.range_start AND vt.range_end
0
k1ng87Author Commented:
you have a range_start and range_end..i'm assuming i'd have to breakout the range into columns?
0
Scott PletcherSenior DBACommented:
That would be ideal.

But if you're always using the "-" to indicate the range, you could do this:

LEFT OUTER JOIN vlookup_table vt ON
    mt.search_value BETWEEN LEFT(vt.range, CHARINDEX('-', vt.range) - 1) AND SUBSTRING(vt.range, CHARINDEX('-', vt.range) + 1, 100)

To simplify coding, if you have only a single value in the range, still specify it as a range, like so:

1-4,
5-5,
6-9,
...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.