Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql - adding time interval with skipping of certain period

I have a mySql query which adds certain interval of time to datetime field.

UPDATE table T 
   SET T.ending = DATE_ADD(T.ending, INTERVAL T.hours * 3600 * some_other_variable_factors SECONDS)) 

Now, I need to detect if new ending time is in between some hours (let's say 20:00 to 06:00), which should be excluded from calculation.

Ie. if old ending is today, 19:58 and we are adding 4 minutes, new ending should be tomorrow, 06:02

Additional difficulty is that amount of time to add can be bigger than 24 hours. So if old ending is today, 19.00 and we are adding 24 hours, new ending should be day after tomorrow, 15.00 (which sounds as a title of a really bad movie ;)

Is there way to achieve this in mysql? In one query? I was also thinking about stored procedures, but i do not have any experience with.

Some test data:

   CREATE TABLE IF NOT EXISTS `tt` (
      `source` datetime NOT NULL,
      `hours` int(11) NOT NULL,
      `off_start` int(11) NOT NULL,
      `off_long` int(11) NOT NULL,
      `correct` datetime NOT NULL    
    ) ENGINE=InnoDb;


    INSERT INTO `tt` (`source`, `hours`, `off_start`, `off_long`, `correct`) VALUES
    ('2010-11-11 12:00:00', 1, 20, 10, '2010-11-11 13:00:00'),
    ('2010-11-11 19:00:00', 1, 20, 10, '2010-11-12 06:00:00'),
    ('2010-11-11 19:00:00', 2, 20, 10, '2010-11-12 07:00:00'),
    ('2010-11-11 19:00:00', 3, 20, 10, '2010-11-12 08:00:00'),
    ('2010-11-11 19:00:00', 24, 20, 10, '2010-11-13 15:00:00'),
    ('2010-11-11 19:00:00', 48, 20, 10, '2010-11-15 11:00:00'),
    ('2010-11-11 19:00:00', 72, 20, 10, '2010-11-17 07:00:00');
like image 205
ts. Avatar asked Nov 11 '10 11:11

ts.


People also ask

How do you add missing values in SQL?

In SQL, due to lack of data, we sometimes need to insert rows with NULL values in the tables. Here, the keyword NULL(without quotes) is used to represent no data.

How do you add time in SQL?

SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.


1 Answers

SELECT  CASE
        WHEN HOUR((t_ending + INTERVAL some_other_variable_factors HOUR)  - INTERVAL 20 HOUR) < 10 THEN
                t_ending + INTERVAL some_other_variable_factors HOUR + INTERVAL 10 HOUR
        ELSE
                t_ending + INTERVAL some_other_variable_factors HOUR
        END
FROM    mytable

INTERVAL 20 HOUR means your off time starts at 20:00, INTERVAL 10 HOUR means it lasts for 10 hours (20:00 till 06:00). Adjust accordingly.

Update:

SET @hours = 54;

SELECT  CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR AS DATETIME);

--
2010-01-03 21:00:00


SELECT  CASE
        WHEN HOUR(CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR AS DATETIME)  - INTERVAL 20 HOUR) < 10 THEN
                CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR + INTERVAL 10 HOUR AS DATETIME)
        ELSE
                CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR AS DATETIME)
        END;

--
2010-01-04 07:00:00
like image 143
Quassnoi Avatar answered Sep 27 '22 18:09

Quassnoi