2

I want to write an update query in MS Access 2003. I have a field called product_code.

  • If product code is (between 110 and 752) OR (between 910 and 1124), I want to update product code=15.
  • If product code is (between 1210 and 1213) OR (1310 and 1423) I want to assign product code=16.
  • If product code is some other value I will assign 18, and so on

I don't think I can use CASE statement for this since I have many values to be updated. I tried to use multiple UPDATE/SET statements but it didn't work.

1
  • 1
    just to be clear, you want to consolidate product code 110-752 and 910-1124 into the product code 15? Commented Jan 2, 2011 at 14:44

2 Answers 2

3

You could create a temporary table to hold your ranges and values. Call it TableB with 3 fields LowId, HighId, NewId

UPDATE TableA SET TableA.ProductID=TableB.NewId
WHERE TableA.ProductID>=TableB.LowID AND TableA.ProductID<=TableB.HighId
Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer I would choose, unless I only had one or two different numbers to change.
2

You can use nested IIF statements to achieve this:

UPDATE  Products
SET Product_Code = IIF(
    (Product_Code BETWEEN 110 and 752) or 
        (Product_Code BETWEEN 910 AND 1124), 
    15,  
    IIF(
        (Product_Code BETWEEN 1210 and 1213) or 
        (Product_Code BETWEEN 1310 AND 1423), 
        16, 18
       )
    )

What this does is if your Product Code is between 110 and 752 or 910 and 1124, it will update the Product_Code to 15. Otherwise, it checks to see if your code is between 1210 and 1213 or 1310 and 1423, in which case it uses 16. Otherwise, it uses 18.

NOTE: If you are updating an ID like this, make sure you have a backup of the database, or at least the table, before running this, as the previous values will be overwritten.

2 Comments

Thanks for the quick reply. I tried your codes and it asks for the parameter value. I am just a beginner, so I don't know what this is!?
@Ozlem -- yes, my table name was just an example since you didn't specify a table name or show any code. You'll have to modify the query to use your table name.

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.