MySQL- How can I extract the monday given a number of week? - mysql

Essentially what I want to obtain is the monday given the number of the week::
I have week: 9, 7,5
This number of week corresponds to a timestamp:
2019-03-02 02:48:00,
2019-02-15 02:58:00,
2019-01-31 00:25:00
I want to obtain the date of the monday of this week(first day of week):
2/25,
2/11,
1/28
How can extract this outout? it can be extracted from timestamp if it is easier

If you want to get Monday of the week.
You can try to use DATE_ADD and WEEKDAY function.
SELECT DATE_ADD(dt, INTERVAL - WEEKDAY(dt) DAY)
FROM T
sqlfiddle
EDIT
There is another function DATE_FORMAT represent to the date format string.
using "%m/%d"
SELECT DATE_FORMAT(
DATE_ADD(dt, INTERVAL - WEEKDAY(dt) DAY), "%m/%d")
FROM T
sqlfiddle

Related

Date of first day in the current quarter in MySQL

Using current_date in MySQL to get today's date, I want to get the current quarter first day date.
Input: 2022-11-01
Output: 2022-10-01
Sample Code
SELECT CURRENT_DATE; (result: YYYY-MM-DD format)
I want to get the current quarter first day's date.
If today's date is 2022-11-09 then current quarter will be October(Q4) and it's first day's date would be 2022-10-01 (1st October)
Use QUARTER function which is a part of MySQL.
This returns you the number of current quarter. Than you can use MAKEDATE to construct your date.
SELECT
MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER - INTERVAL 1 QUARTER

Query of current date in time mysql

I have a column of time with type datetime in mysql called createdAt, so I want find out the data with time range like this
createdAt >= SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate()))
and createdAt <= ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate()))
assume createdAt curdate() based on this question made are october 8th 2020, so that time range will be 28th september 2020 until 04th october 2020
But when I check manually, if I add some time range in another media, why the counting is little bit more different? For example: using this query, count of transaction is 87 transaction, but then I used another media to count the transaction between 28th september 2020 00:00:00 until 04th october 23:59:59 its 90 transaction, is that any I can add in query for time range in my query date range?
Your expression ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate())) returns only date part of last week day, so all data after start this day is not included.
You have couple of options to solve this:
Round createdAt to date withot time part:
SELECT *
FROM your_table
WHERE DATE(createdAt) BETWEEN
SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate())) AND
ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 6-WEEKDAY(curdate()));
Use strong less condition with next day:
SELECT *
FROM your_table
WHERE
createdAt >= SUBDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), WEEKDAY(curdate())) AND
createdAt < ADDDATE(DATE_SUB(curdate(), INTERVAL 1 WEEK), 7-WEEKDAY(curdate()));

SQL - Last Day of Month

I got a Table which looks like this:
DATE | Number
01-01-16 00:00:00 10
02-01-16 00:00:00 10
03-01-16 00:00:00 11
04-01-16 00:00:00 12
05-01-16 00:00:00 13
....
31-01-16 00:00.00 15
........
29-02-16 00:00:00 18
I got this table for the last few months.
I now want to retrieve the value of the rows, which contain the last day of the previous month and the month before the last month. So for today I would like to retrieve the Value of the 31-1-16 and 29-2-16.
My result should look like:
lastmonth | lastmonth2
18-> Corresponding value to Date: 29-02-16 | 15 -> value for 31-01-16
Would appreciate any help.
Cheers
Here is logic for the last day of this month and the previous month:
select last_day(curdate()) as last_day_of_this_month,
last_day(date_sub(curdate(), interval 1 month)) as last_day_of_prev_month
You can get the last day of any month relative to the current month by changing the "1".
And, I have no idea what date "30-2-16". When describing dates, you should use ISO standard formats. The last day of February 2016 was 2016-02-29.
This is Gordon's code for determining the correct dates plus subqueries to fetch the Number values for those rows:
SELECT
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 1 month)) = date(`DATE`)) as lastmonth,
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 2 month)) = date(`DATE`)) as lastmonth2
FROM DUAL;
Hope that's what you wanted! Works for me in a simple example. I don't know if you need the date() part around DATE but it seemed safest.
SELECT CASE
WHEN last_day(curdate()) = `DATE` THEN number
END as number_last_month,
CASE
WHEN last_day(date_sub(curdate(), interval 1 month)) = `DATE`
THEN number
END as number_last_month2
FROM cc_open_csi_view
I can't test it right now on sqlfiddle.

MySQL year interval that Includes the entire month from the previous year

I have MySQL condition that grabs a time interval from now back x number of months. Typically, this will be set to 13 months so you can compare the current month to that of last year.
'created > DATE_SUB(now(), INTERVAL ' . $timeInterval . ' MONTH)'
So for example last January compared to this January, but I'd like to include all of the previous years month. So instead of January 20, 2015 to January 20, 2016 I would have January 01, 2015 to the current date in January this year until February 1st.
I'd use DATE_FORMAT to make it quick and easy, replace the "day" part of the date with a constant. Then subtract your number of months...
... t.created > DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL ? MONTH
As a demonstration of what is returned by that expression, we can test it using a simple SELECT statement:
SELECT NOW(), DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 12 MONTH
NOW() DATE_FORMAT(NOW(),'%Y-%m-01') - INTERVAL 12 MONTH
------------------- -------------------------------------------------
2016-01-27 21:01:02 2015-01-01
FOLLOWUP
Are you sure you want a "greater than" comparison, rather than a "greater than or equal to" comparison >= ?
There are other approaches to generating that date value to compare to. You could use DATE(NOW()) or CURDATE() to return the current date with no time component.
And use the DAY() function to get the numeric value of the current day, and then subtract that (minus 1) as a number of days. For example, something like this:
>= DATE(NOW()) - INTERVAL DAY(NOW())-1 DAY - INTERVAL 12 MONTH
That seems messier and more complicated. I think it's easier to understand stuffing in the '-01' as the day part.
created > str_to_date(concat(year(now())-1, '-01-01'), '%Y-%m-%d')
Or if you need not all previous year:
select str_to_date(concat(year(now())-1, '-', month(now()),'-01'), '%Y-%m-%d')

MySQL round date to the start of the week and month

For "2012-07-12", how can I get the start of the week, i.e., "2012-07-08", and start of the month, i.e., "2012-07-01"?
First day of the month:
SELECT DATE_FORMAT('2007-07-12', '%Y-%m-01');
output: 2007-07-01
First day of the week:
SELECT DATE_SUB('2007-07-12', INTERVAL DAYOFWEEK('2007-07-12')-1 DAY);
output: 2007-07-08
MySQL reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Same answer as Borophyll's, but I have changed the behavior of the first day of the month to return a date, not just a string which avoids date formatting/parsing mentioned in user151220's answer.
First day of the month:
SELECT DATE_SUB('2007-07-12', INTERVAL DAYOFMONTH('2007-07-12') - 1 DAY);
output: 2007-07-01
First day of the week:
SELECT DATE_SUB('2007-07-12', INTERVAL DAYOFWEEK('2007-07-12') - 1 DAY);
output: 2007-07-08
MySQL reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
For those who need Monday as the first day of the week:
SELECT DATE_SUB('2007-07-12', INTERVAL WEEKDAY('2007-07-12') DAY);
output: 2007-07-09
This relies on the WEEKDAY function, which starts with Monday instead of DAYOFWEEK, which starts with Sunday.
The DATE_FORMAT reply from Borophyll is very good, but gives a string rather than a date. So can't be compared easily.
If you need to use this as a comparison to a date field, use str_to_date to reverse it back to date rather than string.
select x from y where date >= str_to_date( DATE_FORMAT(now()-interval 12 month,'Y-%m-01'), '%Y-%m-%d')
If you are (say) looking at 12 months sales figures, but you want to always start off from the 1st of a month.
This will work if you want to just code it and forget about it, it will use datetime now and always return MTD results-
where date_completed between date_sub(date(now()), INTERVAL dayofmonth(now()) -1 day) and now()