I can easily get the start date and end of week like below.
SELECT
COUNT(*) AS reports_in_week,
DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY),
DATE_ADD(mydate, INTERVAL(7-DAYOFWEEK(mydate)) DAY)
FROM mytable
where WEEK(mydate,1) = '29'
GROUP BY WEEK(mydate,1)
but the problem is that case indexing of column date is not working, if i pass date range between indexing will work, i try to get the start date and end date of week using week number;
I am starter in mysql please help.
If you want to filter on a given week and year, then one option is to use str_to_date() to generate a date range from a year and week number:
where
mydate >= str_to_date('2020-29 Monday', '%Y-%u %W')
and mydate < str_to_date('2020-29 Monday', '%Y-%u %W') + interval 7 day
The %u specifier represents a week number, with Monday being the first day of the week (so this is like WEEK() in mode 1, which your query uses).
The above where predicate would take advantage of an index on mydate - which seems to be your purpose here.
I have three variables
1. Start Date (column in a table)
2. Current Date
3. Start Date + 1 week
I want to write a where clause in sql query in such a way that
Current Date should be between Start Date and (Start Date + 1 week)
and Current Date can be less than Start Date but
not greater than (Start Date + 1 week )
use the between function:
where now() between startDate and endDate
The logic of
Current Date should be between Start Date and (Start Date + 1 week)
and
Current Date can be less than Start Date but not greater than (Start Date + 1 week )
is conflicting, since if currentDate should be between startDate and startDate+ 1week, it will never be less than startDate
If you mean currentDate should be between startDate and startDate+1week write
WHERE DATE(NOW()) BETWEEN `startDate` AND DATE_ADD(`startDate`, INTERVAL 1 WEEK)
If you mean currentDate should be less than startDate+1week write
WHERE DATE(NOW()) <= DATE_ADD(`startDate`, INTERVAL 1 WEEK)
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()
Suppose I have 2011-01-03 and I want to get the first of the week, which is sunday, which is 2011-01-02, how do I go about doing that?
The reason is I have this query:
select
YEAR(date_entered) as year,
date(date_entered) as week, <-------This is what I want to change to select the first day of the week.
SUM(1) as total_ncrs,
SUM(case when orgin = picked_up_at then 1 else 0 end) as ncrs_caught_at_station
from sugarcrm2.ncr_ncr
where
sugarcrm2.ncr_ncr.date_entered > date('2011-01-01')
and orgin in(
'Silkscreen',
'Brake',
'Assembly',
'Welding',
'Machining',
'2000W Laser',
'Paint Booth 1',
'Paint Prep',
'Packaging',
'PEM',
'Deburr',
'Laser ',
'Paint Booth 2',
'Toolpath'
)
and date_entered is not null
and orgin is not null
AND(grading = 'Minor' or grading = 'Major')
and week(date_entered) > week(current_timestamp) -20
group by year, week(date_entered)
order by year asc, week asc
And yes, I realize that origin is spelled wrong but it was here before I was so I can't correct it as too many internal apps reference it.
So, I am grouping by weeks but I want this to populate my chart, so I can't have all the beginning of weeks looking like different dates. How do I fix this?
If the week starts on Sunday do this:
DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY)
If the week starts on Monday do this:
DATE_ADD(mydate, INTERVAL(-WEEKDAY(mydate)) DAY);
more info
If you need to handle weeks which start on Mondays, you could also do it that way. First define a custom FIRST_DAY_OF_WEEK function:
DELIMITER ;;
CREATE FUNCTION FIRST_DAY_OF_WEEK(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN SUBDATE(day, WEEKDAY(day));
END;;
DELIMITER ;
And then you could do:
SELECT FIRST_DAY_OF_WEEK('2011-01-03');
For your information, MySQL provides two different functions to retrieve the first day of a week. There is DAYOFWEEK:
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.
And WEEKDAY:
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
If week starts on Monday
SELECT SUBDATE(mydate, weekday(mydate));
If week starts on Sunday
SELECT SUBDATE(mydate, dayofweek(mydate) - 1);
Example:
SELECT SUBDATE('2018-04-11', weekday('2018-04-11'));
2018-04-09
SELECT SUBDATE('2018-04-11', dayofweek('2018-04-11') - 1);
2018-04-08
Week starts day from sunday then get First date of the Week and Last date of week
SELECT
DATE("2019-03-31" + INTERVAL (1 - DAYOFWEEK("2019-03-31")) DAY) as start_date,
DATE("2019-03-31" + INTERVAL (7 - DAYOFWEEK("2019-03-31")) DAY) as end_date
Week starts day from Monday then get First date of the Week and Last date of week
SELECT
DATE("2019-03-31" + INTERVAL ( - WEEKDAY("2019-03-31")) DAY) as start_date,
DATE("2019-03-31" + INTERVAL (6 - WEEKDAY("2019-03-31")) DAY) as end_date
select '2011-01-03' - INTERVAL (WEEKDAY('2011-01-03')+1) DAY;
returns the date of the first day of week. You may look into it.
This is a much simpler approach than writing a function to determine the first day of a week.
Some variants would be such as
SELECT DATE_ADD((SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1)DAY),INTERVAL 7 DAY) (for the ending date of a query, such as between "beginning date" and "ending date").
SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) DAY (for the beginning date of a query).
This will return all values for the current week. An example query would be as follows:
SELECT b.foo FROM bar b
WHERE b.datefield BETWEEN
(SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) DAY)
AND (SELECT DATE_ADD((SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1)DAY),INTERVAL 7 DAY))
This works form me
Just make sure both dates in the below query are the same...
SELECT ('2017-10-07' - INTERVAL WEEKDAY('2017-10-07') Day) As `mondaythisweek`
This query returns: 2017-10-02 which is a monday,
But if your first day is sunday, then just subtract a day from the result of this and wallah!
If the week starts on Monday do this:
DATE_SUB(mydate, INTERVAL WEEKDAY(mydate) DAY)
SELECT MIN(DATE*given_date*) FROM *table_name*
This will return when the week started at for any given date.
Keep the good work going!
Is it possible to do sql query on what day is today and get the row of today for date column?
So let say today is july 25th, i have database table sales, column name date, date = sales transaction date in timestamp.
i need all row that the sales date is same with current date and also is it possible set value as gmt+5?
This will get you all the rows for today's date:
SELECT * FROM sales
WHERE DATE(NOW()) = DATE(DATE_ADD(sales_transaction, INTERVAL 5 HOUR))
ORDER BY sales_transaction
As for GMT +5, Do you mean all rows with a sales date +5 hours or today +5 hours?
EDIT: Updated to add 5 hours to sales date. For a column called date, I would use the back-ticks to indicate it's a column name. e.g. SELECT `date`` FROM sales
I can't figure out how to work the back-ticks on the date field. But you should get the idea. Wrap your column names with `
Give some time and Check out Date Time Function of mySQL
SELECT * FROM tablename WHERE salesdate = '1998-1-1';
If your date is stored in GMT timezone
Select *
FROM tablename
WHERE DATE_ADD(NOW(), INTERVAL 5 HOUR) = salesdate
SELECT * FROM `sales` WHERE DAYOFMONTH(FROM_UNIXTIME(date)) = DAYOFMONTH(DATE(now()))
seems working.
Thank you for all of your replies.