2

oracle timestamp example: 26-APR-17 09.40.13.243356000

Hi i have a scenario I am converting oracle timestamp to postgres timestamp but the output is not expected .

Query run in psql

migration=# SELECT TO_TIMESTAMP('26-APR-17 09.40.13.243356000','DD-MON-YY HH24.MI.SS:MS') :: timestamp without time zone;
        to_timestamp
----------------------------
   2017-04-26 09:15:55.864128                  ----- this is output 
(1 row) 

The output should be: 2017-04-26 09:40:13.243356

enter image description here

3
  • No database is going to arbitrarily add a half hour. This sounds like a timezone issue, if one of the two databases uses the Indian timezone. You didn't post the actual code though. The values you posted look like the localized result of some query, not actual values. Very few countries use such delimiters. Only India uses a half-hour offset (that I know of) Commented Jun 8, 2017 at 10:09
  • 1
    To avoid localization issues use the ISO8601 format, ie YYYY-MM-DDTHH:MM:SS.ffffZ or YYYY-MM-DDTHH:mm:ss.ffff+HH:mm Commented Jun 8, 2017 at 10:10
  • @PanagiotisKanavos look at the picture . I have edited the question. Commented Jun 8, 2017 at 10:26

2 Answers 2

3

The problem is that 243356000 is taken as milliseconds, which accounts for the strange offset of about half an hour.

Trim the last three sigits and use US for microseconds:

SELECT to_timestamp(
          regexp_replace(
             '26-APR-17 09.40.13.243356000',
             '\d{3}$',
             ''
          ),
          'DD-MON-YY HH24.MI.SS.US'
       );
Sign up to request clarification or add additional context in comments.

Comments

2

The error is a result of the invalid value for the milliseconds format mask MS.

As documented in the manual the allowed values for milliseconds are from 000 to 999

.243356000 is however also not valid for microseconds (US format mask) because of the trailing zeroes.

If you want to use that format, you have to remove the zeros from the input string, e.g: using rtrim()

psql (9.6.3)
Type "help" for help.

postgres> SELECT TO_TIMESTAMP(rtrim('26-APR-17 09.40.13.243356000','0'),'DD-MON-YY HH24.MI.SS.US');
         to_timestamp
-------------------------------
 2017-04-26 09:40:13.243356+02
(1 row)

postgres>

I also agree with PanagiotisKanavos that it would be better to use a locale independent format (i.e. numbers for the month and a 4 digit year), preferably an ISO standard for timestamp values.

1 Comment

That will lead to wrong results if the last three digits are not all 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.