finding records within a week, i tried this
SELECT * FROM tblbulletin WHERE YEARWEEK(publisheddate) = YEARWEEK(CURRENT_DATE)
but it returned the records as like calendar week. I.E. the records were between sunday and saturday of a current week.
how can i write a code to set different start of week? and how to display records within custom start of week to custom end of week?
You can include a value for the mode argument which will set what the first day of the week is. If no mode is included the default is to select Sunday as the first day.
See http://www.techonthenet.com/mysql/functions/yearweek.php for a list of the different modes available.
as #gerard says, use YEARWEEK with the optional {mode} value set to 1 - for week starting monday e.g.
SELECT * FROM tblbulletin WHERE YEARWEEK(publisheddate,1) = YEARWEEK(CURRENT_DATE,1)
NOTE: WEEK has a table with the other possible values for {mode}
Related
I am trying to display all users whose birthday is within this week (Monday to Sunday). After searching everywhere I was able to get this but it's displaying users from last week and this week also.
SELECT * FROM teachers WHERE WEEK(birthday, 0) = WEEK(NOW(), 0)
Change the second argument for the WEEK function. I think that you want it to be either 3 or 5. Based on the documentation.
Setting the mode to 0 would get people whose birthdays are on Sunday in the previous week based on what you are saying in your question.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
Check MySQL's function WEEKOFYEAR. You could use it like this to get users with birthday in current week:
SELECT * FROM teachers WHERE WEEKOFYEAR(birthday) = WEEKOFYEAR(NOW())
Use function WEEKOFYEAR:
SELECT * FROM teachers WHERE WEEKOFYEAR(birthday) = WEEKOFYEAR(NOW());
WEEKOFYEAR returns a number in the range 1 to 53.
I have a mysql table which stores users' availability, stored in 'start' and 'end' columns as date fields.
I have a form where other users can search through the 'availabilty' with various periods like, today, tomorrow and next week . I'm trying to figure out how to construct the query to get all the rows for users who are available 'next month'.
The 'start' values maybe from today and the 'end' value might might be three months away but if next month falls between 'start' and 'end' then I would want that row returned.
The nearest I can get is with the query below but that just returns rows where 'start' falls within next month. Many thanks,
sql= "SELECT * FROM mytable WHERE start BETWEEN DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)),INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AND LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH))";
As you are interested in anything that happens in the full month following the current date you could try something like this:
SELECT * FROM mytable WHERE
FLOOR(start/100000000)<=FLOOR(NOW()/100000000)+1 AND
FLOOR( end/100000000)>=FLOOR(NOW()/100000000)+1
This query make use of the fact that datetime values are stored in MySql internally as a number like
SELECT now()+0
--> 20150906130640
where the digits 09 refer to the current month. FLOOR(NOW()/100000000) filters out the first digits of the number (in this case:201509). The WHERE conditions now simply test whether the start date is anywhere before the end of the next month and the end date is at least in or after the period of the next month.
(In my version I purposely left out the condition that start needs to be "after today", since a period that has started earlier seems in my eyes still applicable for your described purpose. If, however, you wanted that condition included too you could simply add an AND start > now() at the end of your WHERE clause.)
Edit
As your SQLfiddle is set-up with a date instead of a (as I was assuming) datetime column your dates will be represented differently in mumeric format like 20150907 and a simple division by 100 will now get you the desired month-number for comparison (201509):
SELECT * FROM mytable WHERE
FLOOR(start/100)<=FLOOR(NOW()/100000000)+1 AND
FLOOR( end/100)>=FLOOR(NOW()/100000000)+1
The number returned by NOW() is still a 14-digit figure and needs to be divided by 100000000. See your updated fiddle here: SQLfiddle
I also added another record ('Charlie') which does not fulfill your requirements.
Update
To better accommodate change-of-year scenarios I updated my SqlFiddle. The where clause is now based on 12*YEAR(..)+MONTH(..) type functions.
I am having a table as follows in MYSQL:
proj_id|hoursWorked|Date.
The date field is of type Date; I want to retrieve all the entries from a table depending on a given week number for the project in my java based web application. Please help me to achieve this.
I am unable to write a single query that will allow me to do so.
Do not use something like WHERE WEEK(column)=something - this is a performance killer: It will calculate the week number on all rows, even if they don't match. In addition to that it will make it impossible to use an index ont this column.
Instead calculate an absolute begin and end date or point in time, depending on your data type, then use BETWEEN. This will do no calculations on non-matching rows and allow the use of an index.
Rule of thumb: If you have the choice between a calculation on a constant and on a field, use the former.
use MySQL WEEK() function.
SELECT WEEK(dateColumn)
FROM...
WHERE WEEK(dateColumn) = 1
WEEK()
from MySQL Docs
This function returns the week number for date. The two-argument form
of WEEK() enables you to specify whether the week starts on Sunday or
Monday and whether the return value should be in the range from 0 to
53 or from 1 to 53.
Use WEEK
select * from your_table
where week(`Date`) = week('2012-12-01')
If you want to get only records from the current week you can do
select * from your_table
where week(`Date`) = week(curdate())
I really don't know how to ask this question or title it but here I go. I work in a school system and I have created a database for some psychologists to use to track their referrals. By state rules,they have 60 days from the date of their first meeting to finish the process. Weekends still count but HOLIDAYS DO NOT. I do not really know how to use the calender we have so that we have an accurate Calculation. For instance, with Holidays accounted for, if a kid was started today, he would need to have everything finished on 1/18/2013 That is 60 days from now based on our schedule. Does anyone have any idea where I should start?
Edit
Ok, so I now have a Calender table. Here is my issue. I have my column that I used to indicate which days are used in calculating my 60 days. Weekends can be used in that calculation. HOWEVER, they cannot be used in the result. If the 60th day lies on a Sunday or Saturday, then the date would need to go to the Friday before. I guess my first issue is really, how do I limit my calculation to the dates in my calender table?
This can be easy with a calendar table.
PARAMETERS start_date DateTime;
SELECT TOP 1 sub.the_date
FROM
(
SELECT TOP 60 the_date
FROM tblCalendar
WHERE
the_date>=[start_date]
AND work_day=True
ORDER BY the_date
) AS sub
ORDER BY sub.the_date DESC;
That query is based on the assumption you have set work_day to True for the dates you want evaluated. IOW, work_day will be False only for your organization's holidays.
For sample code to create and load your calendar table, see the CreateTable_calendar() and LoadCalendar() procedures at Using Start Date and End date in Access query. To initially assign all dates including weekend days as work days, make this change in LoadCalendar().
'rs!work_day = Not (Weekday(dte) = vbSunday Or _
' Weekday(dte) = vbSaturday)
rs!work_day = True
Finally, manually edit the table to change work_day to False for your holidays.
You can check the weekday to ensure you have not chosen a weekend:
SELECT TOP 1 CalDate, WDay
FROM (SELECT Top 60 c.CalDate,Weekday([Caldate]) AS WDay
FROM Calendar c
WHERE c.Holiday=False) a
WHERE WDay Not In (1,7)
ORDER BY CalDate DESC
I have a staff timesheet table where i have timestamp of when those records are created. I now want to generate the report so that my start date is Tuesday and end date is next Monday, which is 1 week. Now i need to generate all the records grouped by this weeks time but will be next set of tuesday to monday.
This is like normal GROUP BY WEEK(Timestamp) but the WEEK numbers are not the default ones i need to generate the reports in this custom duration. I have a query working for this which groups the record efficiently by Week 1, week 2, week 3 etc.. which is picked from default mysql calendar i guess. How can i change that to generate reports grouped by custom weeks ?
Can you tel me how the following works as how the dates are picked up ?
SELECT WEEK(pw.date) AS Date,DATE_FORMAT(pw.date,'%d-%m-%Y') AS post_date,
SUM(wages) AS amount,SUM(pw.hours) AS hours,SUM(pw.minutes) AS minutes
FROM pos_sessions pw
GROUP BY YEAR(pw.date), WEEK(pw.date) ORDER BY pw.date DESC
I think this Other Solution is what you are looking for...