Date of first day in the current quarter in MySQL - 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

Related

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

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

How to get last day of month in mysql by giving month and year as input

How to get last day of the month in MySQL by providing month and year as input.
Similar example, To get last day of the month by date as input
Eg: SELECT LAST_DAY(?) as lastDate
Format:
Input month and year: '07/2015'
Output Result: day /*Eg: 30,31*/
I have tried with date Format
SELECT LAST_DAY(DATE_FORMAT('09/2015','%m/%Y')) as lastDate but it din't work
You just need to convert your values to a date. Here is one way:
select last_day(date(concat_ws('-', year, month, 1)))
Get last day of current month
SELECT LAST_DAY('2020-06-18') AS 'Result';
Get last day of next month
SELECT LAST_DAY(CURDATE() + INTERVAL 1 MONTH);
Get Last day of previous month
SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH);
set #year:=2010;
set #month:=10;
select last_day(concat(#year,'-',#month,'-01')) as last_day;
SELECT DATE_FORMAT(LAST_DAY('2015-9-1'),'%d')
just add an any number date (day) in the query, and then get last day only using date format %d

Number of sunday, monday, etc before current date in current month using Mysql Query

I have one date field inside table, and i want to find the number of sunday's before current date in current month using mysql.
How can i get the number of sunday, monday, etc before current date in current month ?
You can get the no of Sundays in current month and your date column is less than to current date by using SUM with expression DAYNAME(created) ='sunday' ,using sum with expression will result in a boolean 0 or 1,also make sure you have stored the standard date object in your date field in my case i have used the name as created, and in where clause i have compared the date column with current month dates
SELECT
IFNULL(SUM(DAYNAME(created) ='Sunday'),0) `sundays`
FROM `table`
WHERE created > LAST_DAY(NOW() - INTERVAL 1 MONTH)
AND created < CURRENT_DATE()
After question is edited you can also count no. days by their name as below for a period from month starting to current date
SELECT
IFNULL(SUM(DAYNAME(created) ='Sunday'),0) `sundays`,
IFNULL(SUM(DAYNAME(created) ='Monday'),0) `mondays`,
IFNULL(SUM(DAYNAME(created) ='Tuesday'),0) `tuesdays`,
IFNULL(SUM(DAYNAME(created) ='Wednesday'),0) `wednesdays`,
IFNULL(SUM(DAYNAME(created) ='Thursday'),0) `thursdays`,
IFNULL(SUM(DAYNAME(created) ='Friday'),0) `fridays`,
IFNULL(SUM(DAYNAME(created) ='Saturday'),0) `saturdays`
FROM `table`
WHERE created > LAST_DAY(NOW() - INTERVAL 1 MONTH)
AND created < CURRENT_DATE()

Getting the last month's dates in mysql

I need to get the last month's dates from 1st to current date. Suppose if today's date is March 25th, I need to get the dates from 1st to 25th of february. Suppose if today's date is March 30th, I need to get the dates from 1st to 28/29th Feb, whatever the maximum final date is available. I have searched a lot to get that, but no luck. Can someone please help me how to get this special case done? I am able to do it on another database, but I want to do this on mysql.
Basically what I did for other database is this --> date between date(to_char(date(add_months(DATE(sysdate) ,-1)),'YYYY-MM-01 00:00:00')) and date(add_months(DATE(sysdate) ,-1))
This should do what you want:
WHERE d BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y-%m-01')
AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
You can use DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)). This will automatically limit the result to the last day of the month, so if today's date is March 30th, this will return Feb 28th.

Is there a way to get the First day of the previous month and Last day of current month?

Here I need a function in MySQL where it returns
Dates
Starting date of previous month
Last date of current month.
starting date of previous month
select date_format(curdate() - interval 1 month,'%Y-%m-01 00:00:00')
last date of current month
select date_format(last_day(curdate()),'%Y-%m-%d 23:59:59')
You would use NOW() to get the current date and time. MONTH() to get the current month. And using that value, you can get the previous month and the next month. see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Last day of current month:
select last_day(now())
Starting date of previous month:
select adddate(subdate(last_day(now()), interval 2 month), 1)