Skip to content
Advertisement

SQL Extract Values from Timestamp Difference

I’m trying to extract Days, Hours, Minutes, Seconds from the output of subtracting two timestamps in an Oracle database. I then want to take the extracted values and put them into separate columns. I am able to accomplish this using substr but that doesn’t seem efficient. Is there a more efficient and programmatic way of extracting the values? Below is an example query with the current and desired output.

Example:

Current Output:

Desired Output:

Advertisement

Answer

You can use extract() to pull out the required values from the interval:

Demo on DB Fiddle:

DAYS | HOURS | MINUTES | SECONDS
---: | ----: | ------: | ------:
  16 |     1 |      35 |       0
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement