Query using two column values to create range - sql

I have a table with a start time timestamp column and a length integer column representing minutes. I want to be able to do something like this:
SELECT * FROM table t
WHERE t.start_time + interval 't.length minutes' < '2011-10-21';
But this doesn't work. Is there any way to get a new timestamp from the two column values?
I'm running version 8.3 of postgresql

SELECT *
FROM table
WHERE (start_time + interval '1 min' * length_minutes) < '2011-10-21 0:0'::timestamp;
Notes
Simply multiply your integer with 1-minute intervals and add it to the timestamp.
It is slightly faster to compare the timestamp to a timestamp. A date would have to be cast to timestamp (automatically).

You need to cast t.length as a character and then append it... try this instead?
SELECT *
FROM table t
WHERE
t.start_time
+ cast(cast(t.length as character varying) || ' minutes' as interval)
< '2011-10-21';

Related

Between two Unix dates in Oracle

Thanks to previous question...
I have a more simplified OR statement.
Question is instead of a IN how could I change this to a between?
TO_DATE(TO_CHAR(TO_DATE(''19700101'',''yyyymmdd'') + + (FLOOR(ph.change_date/24/60/60)))) IN (''23-DEC-2020'', ''29-DEC-2020'')
So I want to say between the 23-DEC-2020 and 29-DEC-2020 including both?
Thanks
If you want to use an index on the change_date column then perform the conversion on the literal values and convert them to epoch times (rather than converting the column's epoch time to a date, which would not allow you to use a normal index on the column):
ph.change_date BETWEEN ( DATE '2020-12-23' - DATE '1970-01-01' ) * 86400
AND ( DATE '2020-12-29' - DATE '1970-01-01' ) * 86400 + 86399
change_date seems to be in Unix timestamp format -- the number of seconds since 1970-01-01. I would recommend doing the comparison by converting constant values to the same format:
where ph.change_date >= (date '2020-12-23' - date '1970-01-01') * 24 * 60 * 60 and
ph.change_date < (date '2020-12-30' - date '1970-01-01') * 24 * 60 * 60
Note that this is index (and partition) friendly. And, the second comparison is < on the next day to get the entire day.
If you need to deal with the column as "real" dates, you can add a computed column
alter table t add column change_date_date date generated always as
(cast(date '1970-01-01' as timestamp) + change_date * interval '1' second);
You can then reference change_date_date and even define a an index on it.
You can do it with the between keyword.
For example:
to_date('2021.01.06', 'yyyy.mm.dd') between to_date('2021.01.01', 'yyyy.mm.dd') and to_date('2021.01.31', 'yyyy.mm.dd')
I think we can try like this
where <expression>
between TO_DATE('23-DEC-2020','DD-MON-YYYY') and
TO_DATE('29-DEC-2020','DD-MON-YYYY')
You can convert the epoch time(ph.change_date) to date and then compare as follows:
Date'1970-01-01' + FLOOR(ph.change_date/24/60/60) -- considering that change_date is epoch time
between date'2020-12-23' and date'2020-12-23'

Select entries created after a date, which have big integer timestamp

The table has a created_date column which has big integer as time stamp values. One of the time stamp looks like this 1596007131121. How can I query this?
select count(*) from user where created_date: date >='2020-08-30';
I need to query this.
You can convert that to a proper timestamp using the to_timestamp() function:
select *
from the_table
where to_timestamp(created_date/1000::bigint) >= date '2020-08-30';
But I would highly recommend to convert that column to a proper timestamp column.
I think you want:
select '1970-01-01'::timestamp + (created_date / 1000) * interval '1 second'
If you want this in a where clause, then use:
where created_date >= extract(epoch from '2020-08-30') * 1000
This has the nice feature that you can use an index.

How to add variable minutes to a given date?

I have the next query (I'm using postgresql):
SELECT TIMESTAMP fecha_cita + cast((select tiempo_intervencion from cita_intervencion) as interval)
from cita;
What I'm doing here is basically taking a date like this '2001-09-28 01:00' from the 'cita' table (that's what fecha_cita is) and I want to add more time to this complete date, in this case 'tiempo_intervencion' is something like '120 minutes' but this information is in a different table called 'cita_intervencion', the problem is that since these are variables dates and times and not a fixed date, things like SELECT TIMESTAMP '2010-11-19 01:11:22' + INTERVAL '120 minutes doesn't work for me, I get errors like:
ERROR: syntax error at or near "fecha_cita"
LINE 1: SELECT TIMESTAMP fecha_cita + cast((select tiempo_intervenci...
ERROR: cannot cast type d_entero_p to interval
LINE 1: ...ct tiempo_intervencion from cita_intervencion) as interval) ...
I've looked up on google for some information on this and I was trying to follow this, but I can't find anything that can solve my problem.
You need a join and then cast the string to an interval:
SELECT c.fecha_cita, fecha_cita + ci.tiempo_intervencion::interval
from cita c
join cita_intervencion ci on c.id = ci.cita_id;
This assumes that there is some column in cita_intervencion that links that back to the cita table. If you really do not have that you can do something like this:
SELECT c.fecha_cita, fecha_cita + (select tiempo_intervencion::interval from cita_intervencion)
from cita c
But that will only work if cita_intervencion contains exactly one row.
The casting to an interval will only work if the values in tiempo_intervencion follow the rules for an interval.
You do not need the timestamp keyword for columns already defined as a timestamp that is only needed to introduce a timestamp literal (constant) value.
SELECT TIMESTAMP '2010-11-19 01:11:22' + INTERVAL '120 minutes doesn't work for me
That works if you add the missing ' for the interval literal:
SELECT TIMESTAMP '2010-11-19 01:11:22' + INTERVAL '120 minutes'
You can use:
select DATEADD(MINUTE,120,GETDATE())

Compare date + time with timestamp

I have a table with two temporal columns. First (name is DATE) is storing the date (not including the time part) and therefor the datatype is DATE. Second column (name is TIME) is for storing the time in seconds and therefor the datatype is NUMBER.
I need to compare this two dates with a timestamp from another table. How can I calculate the date of the two columns (DATE and TIME) and compare to the timestamp of the other table?
I have tried to calculate the hours out of the time column and add it to the date column, but the output seems not correct:
SELECT to_date(date + (time/3600), 'dd-mm-yy hh24:mi:ss') FROM mytable;
The output is just the date, but not the time component.
You can use the INTERVAL DAY TO SECOND type:
SELECT your_date + NUMTODSINTERVAL(your_time_in_seconds, 'SECOND') FROM dual;
Example:
SELECT TRUNC(SYSDATE) + NUMTODSINTERVAL(39687, 'SECOND') FROM dual;
The calculated date with time is: 10-11-2013 11:01:27
This is a better idea than dividing your value by 3600 in my opinion, as you have an interval in seconds, so it feels natural to use an interval to represent your time, which can then be easily added to a column of DATE datatype.
Oracle Interval in Documentation
NUMTODSINTERVAL Function in documentation
date + (time/3600) is already a DATE, so you don't need to do to_date(). It does have the time part you added though, you just aren't displaying it. If you want to output that as a string in the format you've shown, use to_char() instead:
SELECT to_char(date + (time/3600), 'dd-mm-yy hh24:mi:ss') FROM mytable;
... except that if time is actually in seconds, you need to divide by 86400 (24x60x60), not 3600. At the moment you're relying on your client's default date format, probably NLS_DATE_FORMAT, which doesn't include the time portion from what you've said. That doesn't mean the time isn't there, it just isn't displayed.
But that is just for display. Leave it as a date, by just adding the two values, when comparing against you timestamp, e.g.
WHERE date + (time/86400) < systimestamp
Try like this,
SELECT TO_DATE('11/11/2013','dd/mm/yyyy') + 3600/60/60/24 FROM DUAL;
Your query,
SELECT date + time/60/60/24 FROM mytable;
try using to_timestamp instead of to_date

Add number of days in given date

In PostgreSQL:
Let say:
SELECT DATE(NOW())+interval '1 day';
It will show date tomorrow.
Problem:
While I having a column duration in table tbl, following SQL does not work :
SELECT DATE(NOW())+interval duration || ' day' from tbl;
I just wondering how to make the '1 day' become a variable instead of a constant.
Do not try to make '1day' a variable, just multiply it by duration variable:
SELECT DATE(NOW()+duration * INTERVAL '1day') FROM tbl;