This question already has an answer here:
- Add 2 months to current timestamp 5 answers
In Oracle, how do I subtract a month from timestamp retaining the millisecond part.
I am using add_months
function but it is truncating the milliseconds. Is there a built in function or I have to manually extract month part of timestamp and add to it?
You can get it done by using year to month interval literal:
Result:
When you use
add_months()
function, data type conversion takes place - the value oftimestamp
data type becomes a value ofdate
data type, which does not have fractional seconds.As @jonearles absolutely correctly pointed out in a comment to the answer, using presented above method to subtract one or more months from a given value of
timestamp
data type preserving milliseconds, may causeORA-01839: date not valid for month specified
.To prevent that error from popping up, the query can be rewritten as follows:
Result: