how can I select in MariaDB between two dates (in this case between two minutes)? I mean I want to select between from now() +5 minutes and now() + 30 minutes.
I tried with this query but no luck.
SELECT req_id FROM info WHERE date(sent_date) BETWEEN (NOW() - INTERVAL 5 MINUTE) AND date(sent_date) < (NOW() - INTERVAL 30 MINUTE)
Thank you so much for helping.
BTW, I tried to search answer to my question in stackoverflow but I don't found.
The syntax of your WHERE clause is off. Use this version:
SELECT req_id
FROM info
WHERE sent_date BETWEEN NOW() - INTERVAL 5 MINUTE AND NOW() - INTERVAL 30 MINUTE;
Related
I need create select for all rows it are older than 15 minutes at the moment.
SELECT last_answer_date FROM messages WHERE last_answer_date > NOW() - INTERVAL '15 minutes';
When I start this select now in 16:51:00 I get result:
2019-01-17 16:25:00
In other words I mean:
Give me all rows where last_answer_date > 16:51:00 - 15 minutes (because now 16:51:00)
Give me all rows `where last_answer_date > 16:36:00 (because now 16:51:00 - 15 minutes = 16:36:00)
And I get 16:25:00 but 16:25:00 < 16:36:00
EDIT: When I change > to < I get 2019-01-16 17:50:27
EDIT2: Example:
SELECT insert_date FROM smev_messages WHERE insert_date < NOW() - INTERVAL '15 minutes';
now = 17:16
change to SELECT insert_date FROM smev_messages WHERE insert_date > NOW() - INTERVAL '15 minutes';
17:17
I think it might be a problem with your timezone.
You can check it with show timezone;
Or simply go with select now() and see if it matches your expectations.
If you have a big table with a lot of data you will get some performance problems because when you call
NOW() - INTERVAL '15 minutes' internally postgress need convert your interval to a date
Postgress Interval
I think a better way to-do that you should do something like that to improve performance, because all dates are long values and you should compare it like long values.
-- 15 minutes in millis = 900000
-- 15 minutes in secods = 900
-- Retrive all data inserted at latest 15 minutes
SELECT last_answer_date FROM messages WHERE (NOW() - last_answer_date) < 900;
-- Retrive all data inserted more than 15 minutes
SELECT last_answer_date FROM messages WHERE (NOW() - last_answer_date) > 900;
Say I want to find orders made recently. I am trying this statement:
SELECT * FROM orders WHERE time < now() - interval '30 second';
This doe not work (It does not select any row.) although the following works:
SELECT * FROM orders WHERE time < now();
Do I need to use a fixed time variable? Like:
SELECT * FROM orders WHERE time < '2018-07-01 12:00:00' - interval '30 second';
I am guessing that I should not use now() in statements because it can be changing during operations ...
I believe you have the comparison in the wrong direction:
SELECT *
FROM orders
WHERE time > now() - interval '30 second';
This returns values in the last 30 seconds.
This assumes that time is of an appropriate date type for the comparison.
I have a table(pkg_date) in redshift. I want to fetch some data for every date for the last 3 months.
Here is my query
select * from pkg_data where scan_date < current_date;
How can I use current_date as a variable in the query itself and run this query for every date from April 1.
I have set a cron job which will run in every hour. In every hour it should run with different current_date
SELECT *
FROM pkg_data
WHERE scan_date > CURRENT_DATE - INTERVAL '3 months'
Be careful — Redshift works in UTC, so the CURRENT_DATE might suffer from timezone effects and be +/- what you expect sometimes.
SELECT
CURRENT_DATE,
(CURRENT_DATE - INTERVAL '3 months')::date
Returns:
2018-06-21 2018-03-21
Also be careful with strange lengths of months!
SELECT DATE '2018-05-31' - INTERVAL '3 months'
returns:
2018-02-28 00:00:00
Notice that it gave the last day of the month (31st vs 28th).
By the way, you can use DATE '2018-05-31' or '2018-05-31'::DATE, and also INTERVAL '3 months' or '3 months'::INTERVAL to convert types.
Use dateadd() for getting date 3 moth old day and GETDATE() for get current date.
ie code will look like.
select * from pkg_data where scan_date < dateadd(month,-3,GETDATE());
for cron refer How to execute scheduled SQL script on Amazon Redshift?
If I write:
SELECT *
FROM table
WHERE date >= NOW - INTERVAL '5 MINTUE';
I will get all the data from the last 5 minutes, but the first and last minute won't be full minutes, they'll likely be partial minutes. Meaning if "NOW()" is 4:30:24, then NOW() - INTERVAL '5 MINUTE' will get me from 4:25:24 to 4:30:24. Minute 25 is partial and only contains 36 seconds, same with minute 30 which only contains 24 seconds.
How would I go about getting the last full 5 minutes? I considered removing the first and last minute or min/max, but that idea became a challenge when I consider minute [58, 59, 60, 01, 02]. In this case min/max doesn't account for the cycle back to 0 after minute 60.
Any ideas would be helpful.
You can use date_trunc():
SELECT *
FROM table
WHERE date >= date_trunc('minute', now()) - interval '5 minute' and
date < date_trunc('minute', now())
Try something like this
SELECT *
FROM table
WHERE date >= DateADD(mi, -5, Current_TimeStamp)
my database is using an integer in epoch time for date in this table.
I want to do something like this:
select * from myTable where date_column > CURRENT_TIMESTAMP - 6 months
I'm not sure how to get 6 months out of this, dynamically. And the result of CURRENT_TIMESTAMP - 6 months would have to be in epoch time
insight appreciated
In Postgres, I believe the correct syntax is:
date_column > EXTRACT('epoch' from (NOW() - interval ' 6 months'))
or similarly:
to_timestamp(date_column) > (NOW() - interval ' 6 months'))
You can read the complete documentation of the date/time functions for Postgres for more information
In MSSQL you can use
select *
from myTable
where date_column > dateadd(month,-6,CURRENT_TIMESTAMP)
You can try this
SELECT *
FROM myTable
WHERE TO_TIMESTAMP(date_column) > CURRENT_DATE - INTERVAL '6 MONTH';
Here is sqlfiddle