Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add time to date

Tags:

date

time

mysql

I have a date range parameter, need to split into multiple date, and each will have same time as the parameter.

Not sure what is the problem with ADDTIME, maybe Im missing a cast? I can solve it casting to string and concat but I guess should be easier.

Current Output

    selectDate  time(@s_date)   time(@e_date)   addtime(date(a.selectDate), time(@s_date))
1   2016-09-03  08:00:00        16:00:00        08:00:00
2   2016-09-04  08:00:00        16:00:00        08:00:00

Desire Output

    selectDate  time(@s_date)   time(@e_date)   addtime(date(a.selectDate), time(@s_date))
1   2016-09-03  08:00:00        16:00:00        2016-09-03 08:00:00
2   2016-09-04  08:00:00        16:00:00        2016-09-04 08:00:00

Possible solution:

CONCAT(DATE_FORMAT(a.selectDate, '%Y-%m-%d '), 
       DATE_FORMAT(@s_date, '%H:%i:%s')) as start_time,

My big query

select a.selectDate,
       time(@s_date),
       time(@e_date),
       addtime(date(a.selectDate), time(@s_date))

from (
    select '1900-01-01' + INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a)) DAY as selectDate
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as e
) a
CROSS JOIN (SELECT @s_date := '2016-09-03 08:00:00', @e_date := '2016-09-04 16:00:00') par
WHERE selectDate BETWEEN date(@s_date) 
                     AND date(@e_date)
like image 770
Juan Carlos Oropeza Avatar asked Nov 08 '22 05:11

Juan Carlos Oropeza


1 Answers

select a.selectDate,
       time(@s_date),
       time(@e_date),
       addtime(concat(a.selectDate,' ','00:00:00'),time(@s_date))

from (
    select '1900-01-01' + INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a)) DAY as selectDate
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as e
) a
CROSS JOIN (SELECT @s_date := '2016-09-03 08:00:00', @e_date := '2016-09-04 16:00:00') par
WHERE selectDate BETWEEN date(@s_date) 
                     AND date(@e_date)

refer this link

like image 95
Prasanna Kumar J Avatar answered Nov 14 '22 21:11

Prasanna Kumar J