简体   繁体   中英

Oracle: calculate day fraction of DATE type column

I have a DATE type column in Oracle table. As I understand it holds information about hours, minutes and seconds. I need to get a day Fraction of this column. Currently I am using this construction:

TO_CHAR(p.snaptime, 'HH24')/24 + TO_CHAR(p.snaptime, 'MI')/1440 + TO_CHAR(p.snaptime, 'SS')/86400

Is there more elegant (=shorter) way to get the same? Preferably even more time/CPU efficient for calculating it by Oracle. Thank you.

Hmmm. How about this?

select (p.snaptime - trunc(p.snaptime))

The difference of two dates is a number. In this case, the number will represent the time of the day. If you wanted this in some other unit, then you can convert by multiplication ( * 24 for hours, * 24 * 60 for minutes, * 24 * 60 * 60 for seconds).

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