Question

When I execute a SQL query using cx_Oracle and the results are 1900-01-01 00:00:00.000000 the result returned is 1900-01-01 00:00:00 without the milliseconds.

If the result contains milliseconds that aren't all zeroes then I get the full timestamp including the milliseconds (i.e. 1900-01-01 00:00:00.123456).

I've tried running ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SSXFF' before running my other query but don't see any changes.

Any ideas?

Was it helpful?

Solution

You and Nagh suspected correctly: it's a Python thing, but it's just a display thing. You're not losing timestamp precision.

The cx_Oracle Python library converts Oracle date types (Date, Timestamp..) into one of Python's date types, datetime.

When a datetime object is printed out as a string, (ie, its __str__() method is called), it prints out in ISO 8601 format, using datetime.isoformat() - this method is where the decision not to print milliseconds when they are 0 is made (actually, a datetime has no millisecond field, it instead has microseconds, but same point).

However, this is just the string representation of the datetime object. All of the time information is still there for you to use.

If you want to display milliseconds all the time you have to be explicit; try using strftime():

from datetime import datetime

now = datetime.now()
print now
# >>> 2013-11-28 15:34:47.223000
now = now.replace(microsecond=0) # Remove milliseconds
print now
# >>> 2013-11-28 15:34:47
print now.strftime('%Y-%m-%d %H:%M:%S.%f')
# >>> 2013-11-28 15:34:47.000000

OTHER TIPS

I guess problem is on receiver side, someone optimizing numbers for you.

You can try to get this timestamp as string and check results

SELECT 
  TO_CHAR(TO_TIMESTAMP('1900-01-01', 'YYYY-MM-DD'), 'YYYY-MM-DD HH24:MI:SSXFF')
FROM dual;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top