0

Im trying to make an query that shows the adresses with the ZIP of 1391, 1393, 1396, 1426, 1427 and all the zips between 3400 and 3433.

SELECT ZIP, HOUSE_NR, APPARTMENT_NR, VerzBedrag
FROM werkelijke_inboedels
WHERE ProductLine = 'inboedel'
AND Product_Option = 'Inboedel All Risk' OR 'Inboedel Extra Uitgebreid'
AND ZIP = %'1391'% OR %'1393'% OR %'1396'% OR %'1426'% OR %'1427'%
AND ZIP > %'3400'%
AND ZIP < %'4133'%

Can someone change the query in the right one?

1
  • why are you wildcarding the ZIP search? Commented Feb 28, 2013 at 17:32

3 Answers 3

4

You only need to use IN and BETWEEN to work the query as expected. Pattern matching is not needed here.

SELECT  *
FROM    tableName
WHERE   ProductLine = 'inboedel' AND 
        Product_Option IN ('Inboedel All Risk','Inboedel Extra Uitgebreid') AND
        (
            ZIP IN (1391, 1393, 1396, 1426, 1427) OR
            ZIP BETWEEN 3400 AND 3433
        )
Sign up to request clarification or add additional context in comments.

6 Comments

The OP doesn't actually want a LIKE. A plain IN without the % should do just fine
@a_horse_with_no_name yes you're right. It just needs IN and BETWEEN
It does not work.. In the result I also get also ZIP codes bigger than 4133 and smaller than 3400
have you tried my updated one. the first query i posted did not group the OR condition.
you're welcome :D by the way, What RDBMS you are using? RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, etc...
|
4
AND ZIP IN ('1391', '1393', '1396', '1426', '1427')
OR  ZIP BETWEEN  '3400' AND '4133'

3 Comments

yeah. I can't see why they're needed anyway
running between on strings is risky business :) Avoid it like the plague.
should be fine if they're all 4-'digit' zips
0

wild cards go inside of quotes:

'%1391%'

These are used with the LIKE operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.