Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date with IIF statement 2

Status
Not open for further replies.

monkeysee

Programmer
Sep 24, 2002
201
US
I have a query that lists the type and dates of inspections for food facilities. Fields used:

FoodID
Priority (this will only have entries: 1, 2, 3)
InspectionDate

I wish to add a field : "Re-Inspection Date"
which will be based on the Priority & InspectionDate fields;

Examples:
If priority = 1, then the next inspection will be 4 months after the InspectionDate.

If priority = 2, then the next inspection will be 1 year after the InspectionDate.

If priority = 3, then the next inspection will be 2 years after the Inspection Date.

Can anyone help with building the "iff" statement or suggest alternative?
 
thanks r937

I used your expression and got some errors - I modified as so:

SELECT Food.BusinessName, Food.Priority, Inspections.InspectionDate, IIf([Priority]=1,DateAdd("mm",4,[InspectionDate]),
IIf([Priority]=2,DateAdd("yyyy",1,[InspectionDate]),
IIf([Priority]=3,DateAdd("yyyy",2,[InspectionDate])))) AS ReInspectionDate
FROM Food INNER JOIN Inspections ON Food.FacilityID = Inspections.FacilityID;

the field returns: #Error

any ideas? (also I should have mentioned, I using Access 2003)
 
And something like this ?
SELECT Food.BusinessName, Food.Priority, Inspections.InspectionDate,
IIf([Priority]=1, DateAdd('m',4,[InspectionDate]),
IIf([Priority]=2, DateAdd('yyyy',1,[InspectionDate]),
IIf([Priority]=3, DateAdd('yyyy',2,[InspectionDate]), [InspectionDate]))) AS ReInspectionDate
FROM Food INNER JOIN Inspections ON Food.FacilityID = Inspections.FacilityID;


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
yeah, i was wrong to use "y" for year, "y" is day of year

but "m" is month, i just looked it up :)

plus, your last IIF -- which is not necessary -- is missing the third parameter

the syntax is IIF(condition,valueiftrue,valueiffalse)

rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (next course starts March 6 2005)
 
r937
Really appreciate the help!
Here is what I have:

SELECT Food.BusinessName, Inspections.InspectionDate, IIf([Priority]=1,DateAdd("m",4,[InspectionDate]),
IIf([Priority]=2,DateAdd("yyyy",1,[InspectionDate])
,DateAdd("yyyy",2,[InspectionDate]))) AS ReInspectionDate
FROM Food INNER JOIN Inspections ON Food.FacilityID = Inspections.FacilityID;


also, i don't really understand why the 3rd parameter isn't needed as priority 2 and 3 differ in length of time of inspection
 
Seems you even not tried my suggestion ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV
Yes, I did,
the field returned: "#error"

also, when using the 3rd parameter as you wrote, I got another error stating duplicate value.

Still open for suggestions!
 
PHV
yes, (refer to the line which addresses Priority 3)

SELECT Food.BusinessName, Food.Priority, Inspections.InspectionDate,
IIf([Priority]=1, DateAdd('m',4,[InspectionDate]),
IIf([Priority]=2, DateAdd('yyyy',1,[InspectionDate]),

[highlight]IIf([Priority]=3, DateAdd('yyyy',2,[InspectionDate]), [InspectionDate])))[/highlight]

AS ReInspectionDate
FROM Food INNER JOIN Inspections ON Food.FacilityID = Inspections.FacilityID;

I eliminated the second [InspectionDate]
then I was back to the #Error message.
 
And you get a 'Duplicate value error' simply executing this SELECT instruction ?
 
r937
oops forgot:
datatype of InspectionDate is date (short date) mm/dd/yyyy
 
And this ?
SELECT F.BusinessName, F.Priority, I.InspectionDate,
IIf(F.Priority=1, DateAdd('m',4,I.InspectionDate),
IIf(F.Priority=2, DateAdd('yyyy',1,I.InspectionDate),
IIf(F.Priority=3, DateAdd('yyyy',2,I.InspectionDate), I.InspectionDate))) AS ReInspectionDate
FROM Food F INNER JOIN Inspections I ON F.FacilityID=I.FacilityID;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
r937

That did it! THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
r937
Can you help with this scenario?

Same Date & Iff statement, however, I wish to add one more condition.

I need the calculation to perform only if the field: [Inspection.Type] = "routine"

If it doesn't = routine, then I want the user to be able to fill in date of choosing.

Thanks a bunch for any help you can give.!
 
PARAMETERS [Enter new inspection date] DateTime;
SELECT F.BusinessName, F.Priority, I.InspectionDate,
IIf(I.Type='routine',
IIf(F.Priority='1', DateAdd('m',4,I.InspectionDate),
IIf(F.Priority='2', DateAdd('yyyy',1,I.InspectionDate),
IIf(F.Priority='3', DateAdd('yyyy',2,I.InspectionDate),
I.InspectionDate))), [Enter new inspection date]) AS ReInspectionDate
FROM Food F INNER JOIN Inspections I ON F.FacilityID=I.FacilityID;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top