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.
Related
I try to find a function where I can extract the result of the last month only
(for exemple if I launch the query in november, I want to display only the resultat of october)
There the result :
I dont know if I have to enter the function in my select or where clause
Thanks for you help!!
CHeers!
I tried the function month(date, -1)
I want to see all the result for the previous month
You can try getting the date and applying in the where clause.
Note that there may be more efficient options available.
Query to Use:
WHERE
DATE_TRANSACTION BETWEEN
trunc(date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)),'MM')
AND
date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE))
Explanation:
CURRENT_DATE - Gives the current Date
dayofmonth(CURRENT_DATE) - Gives the day part of current date.
date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)) - Gives the last day of previous month. Assume current_date is 2022-11-23, dayofmonth will give 23, when you subtract 23 days, it goes to the last day of previous month i.e. 2022-10-31.
trunc(date_sub(CURRENT_DATE, dayofmonth(CURRENT_DATE)),'MM') - Truncates date to first day of month.
DATE_TRANSACTION - between these two days.
i got a problem for MySQL , last week , i get the error when run this MySQL script
delete from calendar.schedule
WHERE startdate >= DATE(NOW()) - 7 and status = 'ready'
it will display
#1292 - Incorrect datetime value: '20210100' for column 'startdate' at row 1" error .
my testing date is 2021/1/7 , if i change the Mysql script to
delete from calendar.schedule
WHERE startdate >= DATE(NOW()) - 6 and status = 'ready'
it will work normally. now, this code has no issues. but it will have bugs in the first week of the next year. anyone can help with this? Many Thanks!
Wilson
Your issue is with this expression:
DATE(NOW()) - 7
When you try to subtract 7 from a date, MySQL converts the date to its integer representation (in this case I presume it was 20210107) and then subtract 7 from it, giving 20210100. It then tries to compare this to a datetime column and fails, since 20210100 is not a valid date. The code works when you use 6 because you end up with 20210101, which is valid. What you should be doing instead is subtracting an interval (see the manual) so that you use date arithmetic, not integer arithmetic:
CURDATE() - INTERVAL 7 DAY
Note that CURDATE() is equivalent to DATE(NOW())
If you do date arithmetic with integers as you are doing, the date is converted to an integer in the format YYYYMMDD, and then the value is subtracted.
The problem is this can produce a result integer that is not a valid date.
For example if NOW() is '2021-01-10' as it is right now when I run that expression, then DATE('2021-01-10) - 10 evaluates as 20210110 - 10 which is 20210100.
But there is no date with 00 as the day. The subtraction should be 2020-12-31, right? But when doing integer subtraction, that's not what you get.
Solution: Use date arithmetic, not integer arithmetic. You can write date arithmetic in either of the following ways:
DATE('2021-01-10') - INTERVAL 10 DAY
DATE_SUB('2021-01-10', INTERVAL 10 DAY)
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.
I have this query where I provide to-date & from date.
SELECT *
FROM sales
WHERE date between to-date AND from-date;
Now I want to execute this query with following parameters
to-date = Oct-2015
some-other-date = Oct-2015
That is I want records of the whole month.
How would I do that in a query where I have to and from dates provided it will work for both scenarios where months can be same and different as well.
Update:
dataType for column date is date
You can find the first day of the month containing any given timestamp with an expression like this. For example by using the timestamp NOW(), this finds the first day of the present month.
(DATE(NOW() - INTERVAL DAYOFMONTH(DATE(NOW()))
That's handy, because then you can use an expression like
(DATE(NOW() - INTERVAL DAYOFMONTH(DATE(NOW())) - INTERVAL 1 MONTH
to find the beginning of the previous month if you like. All sorts of date arithmetic become available.
Therefore, you can use an expression like the following to find all records with item_date in the month before the present month.
WHERE item_date>=(DATE(NOW()-INTERVAL DAYOFMONTH(DATE(NOW()))- INTERVAL 1 MONTH
AND item_date < (DATE(NOW()-INTERVAL DAYOFMONTH(DATE(NOW()))
Notice that we cast the end of a range of time as an inequality (<) to the moment just after then end of the range of time.
You may find this writeup useful. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
It's often useful to create a stored function called TRUNC_MONTH() to perform the conversion of the arbitrary timestamp to the first day of the month. It makes your SQL statements easier to read.
select * from sales
where from-date >= 1-Oct-2015
and to-date <= 1-Nov-2015
Update
select * from sales
where date >= from-date
and date <= to-date
Here is SQLFIDDLE
You Can get month from your both to and from dates and find records of that month
SELECT * FROM sales
WHERE MONTH('2002-01-03') AND MONTH('2002-01-3')
SqlFiddle of Using Month Function
I am trying to capture the Monday" date from a giving date.
so today is 4/10/2014 I need to return 4/07/2014 since Monday was 4/07/2014
and if I do 02/07/2014 it should return 02/03/2014 since it was the last Monday.
How can I do that using MySQL?
thanks
Just use WEEKDAY()
SELECT '2014-04-10' - INTERVAL WEEKDAY('2014-04-10') DAY;
(zero is for monday). Substutute your date instead of 2014-04-10 (but it must be valid date). So: you'll subtract number of days past since last monday.
If given date string isn't in standard MySQL date format, use STR_TO_DATE() to convert it