3

I need to find the difference between two timestamps in seconds using JOOQ. I have taken a look at some answers on StackOverflow using raw SQL, however i didn´t find a way to implement it using JOOQ.

Here are some solutions that i found that are using raw SQL:

Tobias Marschall
  • 2,355
  • 3
  • 22
  • 40

1 Answers1

3

Use DSL.timestampDiff(timestamp1, timestamp2). It will return the difference in terms of an INTERVAL DAY TO SECOND type: org.jooq.types.DayToSecond

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • This won't return the diff in seconds, will it? – Tobias Marschall Aug 21 '18 at 15:59
  • @TobiasMarschall: Even better, you'll get an interval type from which you can extract the seconds. I'm happy to answer a new question about some specific usage (which your current question didn't mention), in case this isn't sufficient – Lukas Eder Aug 21 '18 at 17:39
  • Thanks for your answer. As far as i get it, your proposed solution requires me to convert the `org.jooq.types.DayToSecond` field from my result record to seconds using Java. Is it possible to use the DB for converting the interval to seconds? – Tobias Marschall Aug 21 '18 at 21:34
  • I have added some more information to the question. – Tobias Marschall Aug 21 '18 at 21:46
  • @TobiasMarschall: Thanks for your edit. Just a hint for using Stack Overflow: Your edit really changes the question and makes it confusing for future readers. I think it would be better to simply ask a new question and revert this one to the old version, which was useful as well... – Lukas Eder Aug 22 '18 at 06:50
  • I´ve create a follow up question. It would be great if you could take another look :) https://stackoverflow.com/questions/51961787/find-and-sum-difference-between-timestamps-in-seconds-in-postgresql-using-jooq – Tobias Marschall Aug 22 '18 at 07:29