Interval comparison in Select and substraction of interval day to secon
Marius2Dec 26 2006 — edited Dec 26 2006CREATE TABLE some_table
(t1 INTERVAL DAY TO SECOND
,t2 INTERVAL DAY TO SECOND);
What I want to do is this something similar to this, which works for postgresql
SELECT CASE WHEN t1>t2 THEN t1-t2 ELSE null END
FROM some_table;
So I want to compare to columns in the select statement and if one is larger than the other I want the returned tuple to contain one value and if not another one.
I tried the following:
SELECT CASE sign(t1-t2) WHEN 1 t1-t2 ELSE NULL END
FROM some_table;
This does not work because you can not subtract two interval day to second from each other inside the sign. I could probably extract each element of the day to second and to the comparison like this
SELECT CASE sign( (EXTRACT(day from t1)-EXTRACT(day from t2))*60*60*24+EXTRACT(second from t1)-EXTRACT(second from t2)) WHEN 1 THEN (EXTRACT(day from t1)-EXTRACT(day from t2))*60*60*24+EXTRACT(second from t1)-EXTRACT(second from t2) ELSE NULL
but I'm looking for a less messy way to do this.
So my two questions is
1) How can i find the difference between two INTERVAL DAY TO SECOND, that is how much longer one is than the other.
2) Is there another way to do > comparison in SELECT than using minus and sign?