View Full Version : Multiple IF statements in query


joshandsony
04-06-2009, 06:08 PM
Are you able to do multiple if statements within a query? I need to take a due date, and for every "drop shipment" I need to take a week off the due date, for every other shipment I need to do one that takes 2 weeks off the due date. Can someone help me with this?

raskew
04-06-2009, 06:34 PM
Sure -

Here's an example, based on Northwind's Orders table, that creates a calculated field and populating it with [RequiredDate] minus 1 week if [ShipCountry] is Brazil or Germany, or minus 2 weeks for all other [ShipCountry]'s.

SELECT
Orders.OrderID
, Orders.RequiredDate
, Orders.ShipCountry
, DateAdd("ww",IIf([ShipCountry] In ("Brazil","Germany"),-1,-2),[RequiredDate]) AS NewReqDte
FROM
Orders;

You should be able to adapt it to your situation, replacing table/field names and modifying the Iif() statement.

HTH - Bob

joshandsony
04-06-2009, 06:50 PM
So it should look something like this:
IIf( [Drop Ship] = ("Y"),-1,-2), [Due Date] )

raskew
04-06-2009, 06:58 PM
Assuming [Drop Ship] is a Yes/No (Checkbox) field:

DateAdd("ww",IIf([[Drop Ship] = True,-1,-2),[Due Date])

or even (perhaps)

DateAdd("ww",IIf([[Drop Ship],-1,-2),[Due Date])

Bob

joshandsony
04-06-2009, 07:05 PM
Cool that worked. Thank you so much. :-)

joshandsony
04-06-2009, 07:17 PM
Oh one more thing. If I have the due date column in the query, how do I sort that to only show 21 days back from today date and before.

Example: Today is 4/6/09. I need it to show everything previous to 3/23/09

raskew
04-06-2009, 07:23 PM
In the criteria cell of the date field:
<= Date()-21

Bob

joshandsony
04-06-2009, 07:29 PM
Thank you, thank you, thank you. You rock!

raskew
04-06-2009, 07:32 PM
Glad it worked for you.

Note: At my age, I probably more roll than rock.

Bob

joshandsony
04-06-2009, 07:39 PM
Hahaha. That was pretty funny. :-P