select tmp.*, case when pos = 0 and date_add(start_date, pos) < start_date then date_format(start_date, 'yyyy-MM-dd HH:mm:ss') else concat(date_format(date_add(start_date, pos), 'yyyy-MM-dd'), ' 00:00:00') end as mid_start_date, case when pos = datediff(end_date, start_date) and date_add(start_date, pos + 1) > end_date then date_format(end_date, 'yyyy-MM-dd HH:mm:ss') else concat(date_format(date_add(start_date, pos + 1), 'yyyy-MM-dd'), ' 00:00:00') end as mid_end_date from (select work_start_time as start_date, work_end_time as end_date from bst_bas_driver_info_work_time where id in ('2440780')) tmp lateral view posexplode(split(space(datediff(end_date, start_date)), '')) t as pos, val;