I am trying to get a specific record to show that falls between a start date and end date at a specific time.
Here is what I have in MySQL:
SELECT *
FROM OnCallCalendar
WHERE
CallDay <= CURDATE()
AND CallEnd >= CURDATE()
AND callTime < CURTIME()
So a call shift would start on 9/27/2014 at 7:00am and run to 9/28/2014 at 7:00am. The entries in the database would be something like this:
doctor | CallDay | CallEnd | callTime
smith 9/27/2014 9/28/2014 07:00:00
jones 9/28/2014 9/29/2014 07:00:00
SELECT *
FROM OnCallCalendar
WHERE
NOW() BETWEEN CONCAT(CallDay, ' ', CallTime) AND Concat(CallEnd, ' ', CallTime)
Related
I am seeking to find first date of the month in the corresponding table:
So if i have 26/08/2011 August as date and 2 months to add, it becomes 26/10/2011. I need the first date of the resulting month- like 01/10/2011.
Can this be achieved in SQL?
Update : I could get the date of the month using
DATEADD(month,months_add, date)
Couldnt get to "beginning of month". Tried: How can I select the first day of a month in SQL?
But for me it throws the error: function pg_catalog.date_diff("unknown", integer, date) does not exist;
You could try using date_add for add two months and date_sub for sub the days -1
set #my_date = "2017-06-15";
SELECT DATE_SUB( DATE_ADD(#my_date, INTERVAL 2 MONTH),
INTERVAL DAYOFMONTH(#my_date)-1 DAY);
SELECT table.date,
table.month_add,
DATE_FORMAT(table.date + INTERVAL table.month_add MONTH,
'%Y-%m-01') AS beginning_of_month
FROM table
Assuming your date is currently a varchar in dd/MM/yyyy format, you can use STR_TO_DATE to convert it to a DATE type column, then use DATE_ADD with your months_add column to dynamically add months then finally use DATE_FORMAT to display it back in a 01/MM/yyyy format with the first day of the month.
SELECT
Date_Column,
Date_Months_Add,
DATE_FORMAT(DATE_ADD(STR_TO_DATE(Date_Column, "%d/%m/%Y" ), INTERVAL Date_Months_Add MONTH), '01/%m/%Y') AS Date_Beginning
FROM sample
Result:
| Date_Column | Date_Months_Add | Date_Beginning |
|-------------|-----------------|-----------------|
| 26/08/2011 | 2 | 01/10/2011 |
| 25/04/2011 | 1 | 01/05/2011 |
| 16/09/2022 | 3 | 01/12/2022 |
| 14/07/2022 | 4 | 01/11/2022 |
Fiddle here.
Lets say shop is working from 8:00 till 23:00 and we use time format. Then it's easy. Some kind of:
where NOW() > start and NOW() < end
But what if shop working until 1:00am next day? And now exactly 23:00; So 23 > 1. This is will not gonna work.
So how to store and search business time in the correct way? Maybe in the end field better to store difference in seconds or i even don't know...
UPD: If you recommend use timestamp, then how i will find this time after one year, for example? We need to convert all dates to one?
The only solution that i decided use for now.
select * from times where
('05:00:00' between opens::time and closes::time) or
(
closes::time < opens::time and
'05:00:00' >= opens::time and
'05:00:00' > closes::time
) or
(
closes::time < opens::time and
opens::time > '05:00:00' and
closes::time > '05:00:00'
) and dow = 4
So for 13:00:00 - 04:00:00 I have results when variable is:
05:00:00 - no results
12:00:00 - no results
00:00:00 - 1 row
01:00:00 - 1 row
18:00:00 - 1 row
If you have any better idea, please share
The only correct way to store business hours is to use iCalendar RRules and ExDates
Store the rules a table. Use a library (Postgres has a few) to generate opening hours for the upcoming year. Use a materialized view for this
This lets you handle things like holidays, being closed on the last Thursday of every month, etc.
Its a little bit unclear what language you are in. But here are some examples. If it is formattet as dateTime from the server to example C#, then you can use: start> date1 && end < date2.
If using MySQL then check this post: MySQL "between" clause not inclusive?
t=# create table shop(i int, opens time, closes time, dow int);
CREATE TABLE
t=# insert into shop select g+10,'11:00','23:00', g from generate_series(1,5,1) g;
INSERT 0 5
t=# insert into shop select 23,'12:00','13:00', 6;
INSERT 0 1
then your logic would work:
t=# select * from shop where now()::time between opens and closes and extract(dow from now())::int = dow;
i | opens | closes | dow
----+----------+----------+-----
14 | 11:00:00 | 23:00:00 | 4
(1 row)
it is open on Thursday ATM.
and example for Satruday on and not on time:
t=# select * from shop where '2017-08-12 12:59'::time between opens and closes and extract(dow from '2017-08-12 12:59'::timestamp)::int = dow;
i | opens | closes | dow
----+----------+----------+-----
23 | 12:00:00 | 13:00:00 | 6
(1 row)
Time: 0.240 ms
t=# select * from shop where '2017-08-12 13:01'::time between opens and closes and extract(dow from '2017-08-12 13:01'::timestamp)::int = dow;
i | opens | closes | dow
---+-------+--------+-----
(0 rows)
You should use TIMESTAMP as data_type for start and end column. then you can use where NOW() > start and NOW() < end. It will work fine.
Hi i have table with datetime variable.
I was wondering if i can somehow change the datetime column to add 1O minutes to stored date.
Perhaps some trigger has to be involved.
Thanks for help
I like the INTERVAL expr unit notation. It feels more readable to me:
SELECT NOW(),
NOW() + INTERVAL 10 MINUTE;
+--------------------------------+-------------------------------+
| NOW() | NOW() + INTERVAL 10 MINUTE |
+--------------------------------+-------------------------------+
| August, 12 2013 14:12:56+0000 | August, 12 2013 14:22:56+0000 |
+--------------------------------+-------------------------------+
If you want to select existing rows and add 10 minutes to the result:
SELECT the_date + INTERVAL 10 MINUTE FROM tbl;
If you want to alter existing rows stored in a table, you could use:
UPDATE tbl SET the_date = the_date + INTERVAL 10 MINUTE;
If you want increase by force a value by 10 minutes while inserting, you need a trigger:
CREATE TRIGGER ins_future_date BEFORE INSERT ON tbl
FOR EACH ROW
SET NEW.the_date = NEW.the_date + INTERVAL 10 MINUTE
add 10 minute in following way
SELECT ADDTIME(now(), '1000');
I have a table as -
test_table(booking_id, booking_description, start_date, end_date)
Sample Data -
1 | Some booking | 06/30/2013 | 08/01/2013
2 | Some new one | 08/05/2013 | 09/01/2013
3 | Some new two | 09/03/2013 | 09/05/2013
Now I want to generate a monthly xml file from using some java code (No problem in it, I would write), I would be passing the month and year (basically start and end date of the month) to mysql query and I want some table as -
month = 7, year 2013
1 | Some booking | 07/01/2013
1 | Some booking | 07/02/2013
...
Month = 9, year = 2013
2 | Some new one | 09/01/2013
| | 09/02/2013
3 | Some new two | 09/03/2013
...
I was looking to use a java loop from start date to end date and query mysql to find out whether this date comes in the date range or not, if it comes I would add the details else I would add blanks. But that is going to be horrible approach (will go for 30 times mysql look ups) and I am considering it as last option.
Is there any other way around with one or two mysql query and get the data in the format.
EDIT:
month = 7, year = 2013
Select *
from booking_details
where month(start_date) <= 7 and year(start_date) <= 2013 and
month(end_date) >= 7 and year(end_date) >= 2013
I developed this query but still not sure would it over all the possible scenarios.
Based on my understanding of the question you want something like this:
declare #date datetime
Select booking_id, booking_description, start_date --you don't indicate which date field you want in the results
from test_table
where (start_date between #date and date_add(#date, INTERVAL 1 MONTH))
or (end_date between #date and date_add(#date, INTERVAL 1 MONTH))
SQL is probably not exact, I know TSQL not MySQL but this should be close.
Hi I have a table of the following 2 records:
descript | start | end
test 1 | 2011-07-18 14:30:00 | 2011-07-18 17:00:00
test 2 | 2011-07-18 00:00:00 | 2011-07-19 00:00:00
When I tried to do a select, I can't seems to retrieve the 2nd result (test 2) which seems like clearly it is dated 19th of July.
SELECT * FROM event WHERE start >= "2011-07-18 00:00:00" AND end <= "2011-07-18 23:59:59";
Would appreciate any advise.
"2011-07-19 00:00:00" is MORE than "2011-07-18 23:59:59"
By your condition it should be less, so your query does not match test 2.
Your SQL query should be:
SELECT * FROM event
WHERE start >= "2011-07-18 00:00:00"
AND end <= "2011-07-19 00:00:00";
You can just do this :)
SELECT * FROM event
WHERE start BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
AND end BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
This results in the times in between the range you specified for start AND end
you just need to exclude column end, like
SELECT * FROM event
WHERE start>="2011-07-18 00:00:00" AND start<="2011-07-18 23:59:59";