MySql Start time + duration = endtime

Posted on 2012-08-20
Medium Priority
Last Modified: 2012-08-20
Hi there
I have the following problem. A database that has three columns, and the values ¿¿are saved in the format
cal_date -> 20121114
cal_time -> 123000 (gsm, need to add one hour)
cal_duration -> 90

SELECT date (cal_date) AS date, -> 2012-11-14 - ok
time_format (cal_time + 10000, '% H:% i') AS start_time -> 13:30 added one hour ok

The question is how I add cal_duration 90 minutes to cal_time so I get end_time 15:00

thanks in advance
Question by:HelgeLind
  • 2
  • 2
LVL 25

Accepted Solution

lwadwell earned 2000 total points
ID: 38311190
have a look at:
SELECT date (cal_date) AS date,
       time_format(cal_time + 10000, '%H:%i') AS start_time,
       DATE_ADD(STR_TO_DATE(concat(cal_date,cal_time),'%Y%m%d%H%i%s'), interval 60 minute),
       DATE_ADD(STR_TO_DATE(concat(cal_date,cal_time),'%Y%m%d%H%i%s'), interval 60+duration minute)
from (select 20121114 as cal_date, 123000 as cal_time, 90 as duration union all
      select 20121114 as cal_date, 233000 as cal_time, 187 as duration)v

Open in new window


Author Closing Comment

ID: 38311386
Thanks, lot of timesavning for me :)
LVL 25

Expert Comment

ID: 38311417
are your cal_time values numeric or string?  If they do not have leading 0 for times before midday - you may want to do:

Author Comment

ID: 38311735
Quite true, I saw it
80000 - 8 am is not working
110000 - 11 am is working
different lengths of strings

TIME_FORMAT(DATE_ADD(STR_TO_DATE(concat(cal_date,right(concat('00',cal_time),6)),'%Y%m%d%H%i%s'), interval 60 minute ), '%H:%i') as start_time,
TIME_FORMAT(DATE_ADD(STR_TO_DATE(concat(cal_date,right(concat('00',cal_time),6)),'%Y%m%d%H%i%s'), interval 60+cal_duration minute ), '%H:%i') as end_time

now i works :)

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question