I have an interval of 0012-11-03 (year-month-day) and would like to convert it to number of days:
12 * ~365 + Num of days From 01-01 To 11-03 = answer.
Leap year must be accounted for. (answer + 12/4 ??)
The closest thing I could come up with is using TO_DAYS() MySQL 5.1 but that function "does not take into account the days that were lost when the calendar was changed"
Are you really working with dates of 0012-11-03? I.e. the year 12 AD?
The calendar was not changed recently. They're referring to the days lost in the conversion from the Julian calendar to the Gregorian calendar in 1582 AD.
This should work:
SELECT TO_DAYS('2012-11-03') - TO_DAYS('2012-01-01');
Returns 307.
Related
I am trying to get data by week, month and year.
I store date YYYY-MM-DD HH:MM:SS.
What I am doing is below;
Fetch one week old data;
query + AND WEEK(date) = WEEK(CURDATE())
Fetch a month old data;
query + AND MONTH(date) = MONTH(CURDATE())
The thing is I couldnt be able to get the data correct. For instance when I want to get week old data, I am gettin a year old one too.
Is there any other query that I could use? I have tried DATE(NOW()) - INTERVAL 30 DAY. It works but very slow.
Thanks!
I believe that the problem is that the WEEK function returns the week of the year. So, Jan 1st 2017 might be week 1 (also might be week 53 of the previous year depending on the day of the week and how MySQL handles it). But then, Jan 1st of 2016 is also week 1 - just for a different year.
Trying changing it to:
query + AND WEEK(date) = WEEK(CURDATE()) AND YEAR(date) = YEAR(CURDATE())
Also, if you're storing this as a string then definitely change it to a DATETIME
WHERE ...
AND date >= CURDATE() - INTERVAL 7 DAY
AND date < CURDATE()
Gives you the 7 days ending with yesterday. Use other techniques to get a particular month or week.
This technique is also much faster for large tables with a suitable index. Hiding date inside a function, such as WEEK() prevents the use of an index.
So I'm setting up a query where I need to get items whose going to expire in a week before its expiration date(its expiration date is a month from its creation date)
I'm not really familiar with mysql's date and time functions so I'm not so sure of the syntaxes. Much appreciated ahead of time
EDIT: example, an item is created in feb 20th, its expiration date is march 20th. And lets say today is march 13th, my query needs to get the items whose expiration date is next week.
This is what I'm thinking what it may look like
SELECT * FROM ITEMS WHERE NOW() <= DATE_ADD(orders_items.cre_date, INTERVAL 1 MONTH) - 7 days?
you can do it the other way around (less complex)
get the items, whom were created 21 days -or more- ago
SELECT * FROM orders_items WHERE datediff(NOW() , orders_items.cre_date) >= 21
just to explain why 21? it's 3 weeks since create-date which leaves 7 days to expiration date.
you can read more about datediff
Specs: I'm using MySQL 5.6 with SQLWorkbench, SequelPro on OSX Yosemite
Query background: I'm trying to correct a set of TIMESTAMPDIFF durations for weekends and bank holidays. I have 2 stored procedures which are giving me the number of Saturdays, Sundays and Bank Holidays between two dates - these are working fine. To get the corrected TIMESTAMPDIFF, I therefore multiply the number of Saturdays, Sundays and holidays by 24 to get the number of hours to be subtracted, then subtract that number from the TIMESTAMPDIFF.
Example: As an example, if timestamp A is 14:00 on Friday and timestamp B is 14:01 on Tuesday, the raw TIMESTAMPDIFF is 96:01:00. Assuming Monday is holiday and the weekend is 48:00:00, I want to subtract 72:00:00 from 96:01:00, to get the 'business day difference' of 24:01:00.
The problem: When I do something like "96:01:00" - "72:00:00" as date_sub_test, I get 24. I have lost all formatting, including the 01 minute. Duration are not DATETIME, as they don't correspond to calendar dates, so I can't use DATE_ADD / DATE_SUB.
The question: How should I subtract time from a duration, retaining formatting and relevant base 60 system eg 60 minutes in an hour, not 100?
Thanks in advance!
As Jaydee mentions in a comment:
Have you tried the TIMEDIFF function? https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
TIMEDIFF was what I was looking for. I also added in ABS() to make negative time differences positive, and MAKETIME() to create a time from an integer.
Use SEC_TO_TIME and TIME_TO_SEC like
SELECT SEC_TO_TIME(TIME_TO_SEC('96:01:00')-TIME_TO_SEC('72:00:00'))
you can use ABS like this
SELECT SEC_TO_TIME(ABS(TIME_TO_SEC('96:01:00')-TIME_TO_SEC('172:00:00')))
I tried to do the following query to subtract one day from the given date:
DATE(FROM_UNIXTIME(UNIX_TIMESTAMP('2013-04-01') - 86400))
which returns me 2013-03-30, but it should return 2013-03-31.
If i try to subtract one day of 2013-04-02, i get 2013-04-01 correctly returned.
Is this a date bug in mysql?
It's not a bug - what you have found is the missing hour in daylight saving time: a thing unixtime and your calculation is unaware of since you calculate with seconds and not days.
This is exactly why DBMS have special DATETIME datatypes - to handle all the specialities in timezones, leap years, leap seconds, daylight savings and calendars.
Let the database do the work for you - here is an easier and better way to get what you want:
SELECT DATE('2013-04-01') - INTERVAL 1 DAY
Your code makes the assumption that all days have 24 hours. Yesterday, 31st March, had 23 hours in most European countries. To subtract one day you need something like this:
SELECT NOW() - INTERVAL 1 DAY
Why not use the built in DATE_SUB() function?
SELECT DATE_SUB('2013-04-01', INTERVAL 1 DAY)
EDIT: No, This is NOT a date bug in MySQL.
Find the number of days in created field between COUNT(next month (createddate)
How to do this using mysql query Query need for both datatype date and datetime
EX :
created Expirydate
2-01-2011 2-02-2011
3-04-2010 3-02-2011
Result number of days 1
MySQL has a function TO_DAYS(), which for dates after the year 1582 will give you a day number. Two dates can be converted to day numbers, and these can then be substracted. See the manual at http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_to-days.
MySQL has date and time arithmethic using DATE_ADD() or date + INTERVAL 2 DAY. This functionality can be used to find the desired date. See the manual http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
And the difference between a day and that day + INTERVAL 2 DAY is always 2 DAYS, by definition.
You'll use INTERVAL() but you need to be more specific.