Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 Why am I getting Division By Zero Error?

Author  Topic 

Nexzus
Starting Member

14 Posts

Posted - 2012-12-13 : 19:12:59
Creating a simple recurring Chores database.

ChoreName
EveryNDays 'This is greater than zero if the chore rule applies
EveryNWeeks 'This is 0 if EveryNDays is greater than 0
'...
'Someothers
'...
StartOn 'Date to start


When querying what chores to do for today, out of all the chores that should be done every N days.

I started by:
SELECT ChoreName FROM Chores
WHERE (EveryNDays <> 0) AND
(Now() - StartOn) Mod EveryNDays) = 0

Resulted in Division By zero.

Reading up on (the lack of) operator shortcircuiting in SQL, I changed to:

SELECT ChoreName FROM
(SELECT * FROM Chores WHERE EveryNDays <> 0) AS T2
WHERE (Now() - StartOn) Mod EveryNDays) = 0


Same result.

Any thoughts?

*** Edit. I'll add that I'm actually using DateDiff to get the number of days between StartOn and Now, for the sake of clarity, those are omitted.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-12-14 : 05:42:29
It's up to the server how it executes the statement. It has probably spotted that your second one is functionally the same as the first and executed in the same manner - annoying that optimsers are getting smarter.

Maybe something like
where iif (EveryNDays=0,1,(Now() - StartOn) Mod EveryNDays) = 0

not sure about syntax in access

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Nexzus
Starting Member

14 Posts

Posted - 2012-12-17 : 16:57:39
Ah, that works. Thanks much.
Go to Top of Page
   

- Advertisement -