M A
asked on
Date difference in MYSQL with access query
I have a MYSQL database with access forms frontend. i.e. linked tables in access.
Now I would like to get difference of two dates.
Difference between a date from the database and today. Here is the date format.
BTW this database already running with another frontend as well. I want to access the database and trigger date difference as per our convenience.
This is MYSQL database of ManageEngine servicedeskplus.
Now I would like to get difference of two dates.
Difference between a date from the database and today. Here is the date format.
BTW this database already running with another frontend as well. I want to access the database and trigger date difference as per our convenience.
This is MYSQL database of ManageEngine servicedeskplus.
Your date is stored as Unix Time stamp...you just need to convert it to "human readable format" and perform the diff operation
For example :
value : 1514754001000 is actually
select convert_tz(from_unixtime(1 514754001000 ), 'UTC', 'MST') as 'local time'; -->
Take a look if this what you need...i am pretty sure that i have a VBA function to also handle milliseconds)
For example :
value : 1514754001000 is actually
select convert_tz(from_unixtime(1
select convert_tz(from_unixtime(1514754001 ), 'UTC', 'MST') as 'local time';
2017-12-31T14:00:01Z (the last 3 000 should be truncated as they are milliseconds)Take a look if this what you need...i am pretty sure that i have a VBA function to also handle milliseconds)
ASKER
Thanks John for your response.
Will the below VBA help?
http://www.vbforums.com/showthread.php?513727-RESOLVED-Convert-Unix-Time-to-Date
Will the below VBA help?
http://www.vbforums.com/showthread.php?513727-RESOLVED-Convert-Unix-Time-to-Date
I think is somewhat the code i had...if you have massive records to filter out...better to handle them on mysql as your milliseconds are defaulted to zero...it would be much faster...otherwise the above code should work just fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh, you can't do like that. The constants' declarations must be at the top of the module, like:
Option Compare Database
Option Explicit
' Unix Time.
Public Const UtOffset As Long = -25569
Public Const HoursPerDay As Long = 24
Public Const MinutesPerHour As Long = 60
Public Const SecondsPerMinute As Long = 60
Public Const SecondsPerHour As Long = MinutesPerHour * SecondsPerMinute
Public Const SecondsPerDay As Long = HoursPerDay * SecondsPerHour
<snip>
' Functions to follow here
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, that value is way too large.
Did you divide by 1000?
What does UnixDate show if you press the + sign?
Did you divide by 1000?
What does UnixDate show if you press the + sign?
Well, it was the values to see.
However, you don't divide by 1000 as I showed. As long as you don't, you will get that overflow error.
Correct that:
However, you don't divide by 1000 as I showed. As long as you don't, you will get that overflow error.
Correct that:
Days = DateDiff("d", DateUnix(Val([FROMDATE]) / 1000), DateUnix(Val([TODATE]) / 1000))
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
-->Days = DateDiff("d", DateUnix(Val([FROMDATE]) / 1000), DateUnix(Val([TODATE]) / 1000))
Where I will use this
Where I will use this
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I ended up using this. Below is for 90 days before TODATE. All these copied from above.
SELECT maintenancecontract.CONTRA CTNAME, maintenancecontract.TODATE , Round(Date()-DateUnix(Val( [TODATE])/ 1000)+1,0) *-1 AS Days, contract_fields.UDF_CHAR1, contract_fields.UDF_CHAR4
FROM maintenancecontract INNER JOIN contract_fields ON maintenancecontract.CONTRA CTID = contract_fields.CONTRACTID
WHERE (((Round(Date()-DateUnix(V al([TODATE ])/1000)+1 ,0)*-1)=0) );
SELECT maintenancecontract.CONTRA
FROM maintenancecontract INNER JOIN contract_fields ON maintenancecontract.CONTRA
WHERE (((Round(Date()-DateUnix(V
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When I add your above snippet I am not getting the result as expected.
I have one more issue
When I added one more table I am not getting any value
I am getting blank
I have one more issue
When I added one more table I am not getting any value
I am getting blank
SELECT maintenancecontract.CONTRACTNAME, maintenancecontract.TODATE, DateDiff("d",Date(),DateUnix(Val([TODATE])/1000))-1 AS Days, contract_fields.UDF_CHAR1, contract_fields.UDF_CHAR4, DateDiff("d",Date(),DateUnix(Val([TODATE])/1000)) AS days2
FROM maintenancecontract INNER JOIN contract_fields ON maintenancecontract.CONTRACTID = contract_fields.CONTRACTID
WHERE (((DateDiff("d",Date(),DateUnix(Val([TODATE])/1000)))=0));
Probably something in the JOIN criteria is wrong...check the values of the fields you try to join......a common issue is with string values when declared as CHAR and they are padded with blank spaces.
When I add your above snippet I am not getting the result as expected.
That's because you modified it.
ASKER
Apologize Gustav your comment is correct. I modified with your statement and added one more table and it worked.
Many thanks four your help.
Many thanks four your help.
SELECT maintenancecontract.CONTRACTNAME, maintenancecontract.TODATE, DateDiff("d",Date(),DateUnix(Val([TODATE])/1000))-1 AS difference, contract_fields.UDF_CHAR1, contract_fields.UDF_CHAR4, contractnotificationsettings.DAYS
FROM (maintenancecontract INNER JOIN contract_fields ON maintenancecontract.CONTRACTID = contract_fields.CONTRACTID) INNER JOIN contractnotificationsettings ON maintenancecontract.CONTRACTID = contractnotificationsettings.CONTRACTID;
Great! Thanks for the feedback.
ASKER
Pleasure :))
Honestly I removed -1 as it is creating issue for calculation.
For example if an expiry is tomorrow it should show 1 as of now it shows 0 so I removed -1 in the query.
Honestly I removed -1 as it is creating issue for calculation.
For example if an expiry is tomorrow it should show 1 as of now it shows 0 so I removed -1 in the query.
ASKER