简体   繁体   中英

Oracle SQL - Convert a number value into real timestamp (time only - no date)

I've a ETL process reading data from source which is Oracle database and includes a column of type number holding a timestamp (eg '131516' = 'HHMMSS'). In the target which is a postgres database the value should be written in a column of type 'time' expecting the format 'HH:MM:SS'.

I tried different converts with to_char, to_date and to_timestamp, but I don't get a suitable result.

SELECT to_date(to_char(131516, '000000'), 'hh24:mi:ss') from table

This returns unfortunately a timestamp with date, but will have only the time.

How do I get the numeric value as time in format 'HH:MM:SS'?

Thanks.

Pass the numeric value to PostgreSQL in your ETL. There are no colon ( : ) characters in the result of to_char(131516, '000000') , so do not include them in the format mask.

After you convert it to a timestamp , cast the value to time :

select to_timestamp(to_char(131516, '000000'), 'HH24MISS')::time;

 to_timestamp 
--------------
 13:15:16
(1 row)

Please use below casting function in your source query of the ETL process,

SELECT to_char(to_timestamp('131516', 'hh24miss'), 'hh24:mi:ss') from table;

demo https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=5b890d21c0c27ea7be3ac4b42ad813d1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM