Search code examples
sqloracle-databaseoracle8i

ORA-01830: date format picture ends before converting entire input string


Can you please advise on this?

CREATE TABLE TEST2 ( 
JOB_NAME VARCHAR2(50) NULL, 
RUNTIME NUMBER(22) NULL, 
STARTTIME1 VARCHAR2(50) NULL, 
ENDTIME1 VARCHAR2(50) NULL, 
STARTTIME_READ DATE NULL, 
ENDTIME_READ DATE NULL 
) 
GO 

insert into TEST2 values ('TEST JOB',37,'08/18/2015 20:12:24','08/18/2015 20:13:01',null,null)
 go 
insert into TEST2 values ('TEST JOB',37,'08/18/2015 20:12:24','08/18/2015 20:13:01',null,null)
 go 
insert into TEST2 values ('TEST JOB',195,'08/20/2015 19:17:05','08/20/2015 19:20:20',null,null)
 go 
insert into TEST2 values ('TEST JOB',171,'08/19/2015 19:16:52','08/19/2015 19:19:43',null,null)
 go 
insert into TEST2 values ('TEST JOB',195,'08/21/2015 19:19:08','08/21/2015 19:22:23',null,null)
 go 
insert into TEST2 values ('TEST JOB',32,'08/24/2015 19:23:52','08/24/2015 19:24:24',null,null)
 go 
insert into TEST2 values ('TEST JOB',19,'08/26/2015 19:19:00','08/26/2015 19:19:19',null,null)
 go 
insert into TEST2 values ('TEST JOB',92,'08/25/2015 19:18:59','08/25/2015 19:20:31',null,null)
 go 
insert into TEST2 values ('TEST JOB',198,'08/27/2015 19:29:58','08/27/2015 19:33:16',null,null)
 go 
insert into TEST2 values ('TEST JOB',19,'08/28/2015 19:18:13','08/28/2015 19:18:32',null,null)
 go 
insert into TEST2 values ('TEST JOB',44,'08/31/2015 19:49:22','08/31/2015 19:50:06',null,null)
 go 
insert into TEST2 values ('TEST JOB',266,'09/11/2015 19:17:10','09/01/2015 19:21:36',null,null)
 go 
insert into TEST2 values ('TEST JOB',18,'09/20/2015 19:17:33','09/02/2015 19:17:51',null,null)
 go 
insert into TEST2 values ('TEST JOB',33,'09/22/2015 19:16:50','09/03/2015 19:17:23',null,null)
 go 
insert into TEST2 values ('TEST JOB',170,'08/17/2015 19:18:07','08/17/2015 19:20:57

query:

select job_name,to_char(to_date(avg(to_number(to_char(to_date(starttime1,'mm/dd/yyyy hh24:mi:ss'),'SSSSS'))),'SSSSS'),'HH24:MI:SS')AVG_STARTIME
 from TEST2 group by job_name 

Error:

ORA-01830: date format picture ends before converting entire input string

it is working for some records in the table. Those dates exactly looks like the above. I am not getting what is wrong with this date format

Note: I am using oracle 8i


Solution

  • Try this. You had several problems. Format mask for to_number should be '99999' not 'SSSSS' and result of avg function was not a whole number, so to_date was not working. Rounded the value.

        SELECT job_name,
          TO_CHAR(to_date(ROUND(AVG(to_number(TO_CHAR(to_date(starttime1,'mm/dd/yyyy hh24:mi:ss'),
    'SSSSS'),'99999')),0),'SSSSS'),'HH24:MI:SS')AVG_STARTIME
        FROM TEST2
        GROUP BY job_name