Skip to Main Content
  • Questions
  • SUM THE TIMESTAMP WITH VARCHAR2 DATA IN MYSQL

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, maul.

Asked: October 19, 2023 - 2:05 pm UTC

Last updated: October 22, 2023 - 11:50 pm UTC

Version: 23.2.0-17

Viewed 1000+ times

You Asked

Hello experts, i've created table using Oracle APEX that data exist like this

CREATE TABLE "TR_DAILY_ACTIVITY" 
   ( "ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER  NOCYCLE  NOKEEP  NOSCALE  NOT NULL ENABLE, 
 "SHIPMENT_ID" NUMBER, 
 "DATE_PERIOD" VARCHAR2(50), 
 "TURNING_TIME" VARCHAR2(50),  
  PRIMARY KEY ("ID")
  USING INDEX  ENABLE
   ) ;


that said the DATE_PERIOD & TURNING_TIME is using varchar2(50) with the sample of the table like this

ID | SHIPMENT_ID | TURNING_TIME | DATE_PERIOD
1  | 111         | 00:05:43     | 13/04/2022
2  | 123         | 01:00:23     | 13/05/2022
3  | 111         | 03:12:00     | 20/06/2022
4  | 111         | 00:02:23     | 10/04/2022
4  | 123         | 10:22:23     | 12/05/2022



I want to sum TURNING_TIME based on SHIPMENT_ID to get the total hours for each

I'm using this query :

SELECT SHIPMENT_ID, sum(to_number(translate(TURNING_TIME,':','.'))) AS sum_AS FROM TR_DAILY_ACTIVITY group by SHIPMENT_ID;


but the console keep saying invalid number

I've tried several combination to cast/replace the data

How can i sum the TURNING_TIME to get the exact hours?



Thanks in advance

Maul

and Connor said...

Interval aggregation comes in 23c, but until then, you can use this home grown solution

https://asktom.oracle.com/pls/apex/asktom.search?tag=user-defined-aggregates-and-intervals



Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.