An issue has been brought to my attention recently that was to do with the to_date
function returning an ORA-01840
error for certain values. This prompted an investigation which ultimately proved that it was a data issue, however I decided to put together a workaround for future reference anyway.
The error being thrown was:
That's fairly straight forward, it just means that the date I am trying to convert doesn't have enough precision for the format that I am trying to convert it to. The SQL
below demonstrates this...
Interestingly the error disappears when I add a month and day to my date value i.e. 20160101
is OK even though my format expects hours, minutes and seconds too.
My workaround takes advantage of the above find by padding the date up to 8 characters (yyyymmdd
) as required. It consists of two rpad
operations, one for the day
and one for the month
to cater for inputs of the 'yyyy'
This is the end result...
It's not the best solution but will get you out of a bind if you really need to pad dates like this.
Hope you found this post useful...
...so please read on! I love writing articles that provide beneficial information,
tips and examples to my readers. All information on my blog is provided free of
charge and I encourage you to share it as you wish. There is a small favour I ask in return however -
engage in comments below, provide feedback, and if you see mistakes let me know.
If you want to show additional support and help me pay for web hosting and
domain name registration,
donations, no matter how small, are always welcome!
Use of any information contained in this blog post/article is subject to this disclaimer
Other posts you may like...