SQL Statement With Time - mysql

Hello
I need to list the shifts that contains this period between 22:00 and 05:00:
SELECT *
FROM shift
WHERE hourstart>='22:00:00' AND hourend<='05:00:00'
How can i do it?
Thank you

Hours start <= 22 and hoursend >=5 or hoursend >=22 or hours start <=5 or hours start >=22

Related

MY SQL getdate - yesterday to 7 days ago

I have a db with value that I convert to date
CONVERT (VARCHAR(11),DATEADD(day,wo_date,'1971/12/31'),106) AS Date
and I am trying to have 7 days without today. I am ok to get last 7 days with today by using below code
SELECT datediff(day,'DEC 31 1971',getdate())-7
but I can not get rolling last week ie yesterday (Thursday) to - 8 (Wendsday)?
Is this what you are looking for?
DATEDIFF ( datepart , startdate , enddate )
SELECT DATEDIFF(DAY,GETDATE()-9,GETDATE() -1)
--verify
SELECT GETDATE()-9 --2018-01-24 LAST WEDNESDAY
SELECT GETDATE()-1 --2018-02-01 YESTERDAY
Brain fart! Found the solution in rather easy way.
wo_date > (SELECT datediff(day,'JAN 01 1972',getdate())-7)
AND wo_date <= (SELECT datediff(day,'JAN 01 1972',getdate()))
Thanks again for inspirations!

MySQL Select From Prior Week, not just 7 days

I am attempting to get a query that selects the week prior (Sun-Sat). I've fought with this query and the closest I can get is the last 7 days, using the following:
SELECT *
FROM dates
WHERE date BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE();
I'm really unsure how to proceed from here. It seems as if I need to create some kind of relation between CURDATE() and the Saturday before maybe?
Any help is appreciated.
You are after the Week of the year.
Look at the Week Function: WEEK(date[,mode])
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_week
The mode describes how you define the week (which is the start of the week)
WEEK(date,3) is for a week that starts Monday.
SELECT *
FROM dates
WHERE
-- Last Week
WEEK(date,3) = WEEK(CURDATE(),3)-1
AND YEAR(date)= YEAR(CURDATE()) ;
Don't forget the year. Week is just a number between 1 and 52. So the Year is important!
The code above is not correct. it will fail on the last week of the year!

how to get last 24 hours records from mysql DB divided by 60 minutes interval

I want to get last 24 hours data from Mysql Database.
With the condition(WHERE columnname> CURRENT_TIMESTAMP - INTERVAL 24 HOUR), We can get last 24 hours data.
My requirement is for example now the time is '25-05-2015 19:20', I need to group the 24 hours records in the below intervals as
1 . '25-05-2015 19:20' to '25-05-2015 18:20'
2 . '25-05-2015 18:20' to '25-05-2015 17:20'
--------------------------
24 . '24-05-2015 19:20' to '24-05-2015 18:20'
How can I group the 24 hours data to get the above interval result, Please suggest me
You're right, sorry
SELECT COUNT(*), TIMESTAMPDIFF(HOUR, your_date, NOW()) AS hours
FROM your_table
GROUP BY hours
HAVING hours <= 24;
Hope can help you!
Try this!
SELECT COUNT(*), DATEDIFF(NOW(), your_date) AS days
FROM your_table
GROUP BY days
HAVING days <= 24;
:)

Last n weekdays in sql where clausel

We are using MySQL as our database to store messages with timestamps. Is it possible to create a query that returns messages of the last n weekdays?
I.e. if n is 4 and today is Tuesday, I want messages from this weeks Monday, last weeks Friday, last weeks Thursday and last weeks Wednesday .
If you want to do this directly with mysql it would be a little complicated. As Vatev recommended you should calculate date star date in advance, but if you really want to do this, you'll probably need following functions:
ADD_DATE, with INTERVAL -N WEEKS
FLOOR, in C int/int would do just fine
MOD, a % b :)
WEEKDAY
First of all you need should count how many weeks you should go back, that's easy... For you one week = 5 days, that means
weeks = FLOOR(days / 5)
We've taken care of weeks, so we'll now have to work with the rest:
rest = days MOD 5
Now we have two cases, weekend has occurred or no, for the case that there wasn't weekend days are good. We have to add 2 days to skip it. The weekend occurred if (WEEKDAY(now) - rest) < 0
rest = IF( (WEEKDAY(now) - rest) < 0, rest + 2, rest)
And now we can build it to one par (let's assume you have {days} and {rest} pre-calculated):
WHERE date >= ADD_DATE(
ADD_DATE (
{now},
INTERVAL -IF( (WEEKDAY({now}) - {rest}) < 0, {rest} + 2, {rest}) DAYS,
),
INTERVAL -FLOOR({days} / 5) WEEKS
)
The best i can come up with is calculating the start date ({start_date} in the query) in the language of your choice and then running something like this:
SELECT some_things
FROM your_table
WHERE
WEEKDAY(time_column) < 5
AND time_column >= {start_date}
ORDER BY time_column DESC
You can also make a stored function to calculate 'the date x week days ago' and use it for {start_date}.
Have you tried something like this?
SELECT columns
FROM table
WHERE datediff(column_with_timestamp,NOW()) > n
Where N is as you defined above, the number of days you're looking for.
COL >= date_sub( NOW(), interval 1 week) and
weekday( COL ) < 5
date_sub is to seek rows created last week
weekday is to exclude sunday or saturday

MySQL - Age in minutes

This is a pretty simple homework problem that I've been stuck on for a while. Can someone show me how to go about solving this?
A baby is born on March 1 2012 12:00:00. How old will he be in minutes on July 4th 2013 13:30:00?
Use this query
SELECT TIMESTAMPDIFF(MINUTE, '2012-03-01', '2013-07-04')
Use DATEDIFF() to count the difference in days, than multiply it by 24 * 60 * 60 (number of seconds in a day)..