sql statement, now - 6 months in unix/epoch time - sql

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

Related

SQL SELECT WHERE datetime is between NOW() interval

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;

How to subtract days to a Timestamp in CrateDB SQL query?

How can i subtract days to a timestamp in CrateDB SQL query?
Exist something similar to this?
TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 14 DAY)
Don't think there is a built in function but you could do something like this
SELECT DATE_FORMAT(CURRENT_TIMESTAMP - 1000*60*60*24*14) LIMIT 100
in this example (1000 * 60 * 60) * 24 * 14 (24 is to get days and 14 is your number of days)
NB. You can also cast dates into timestamp and perform similar functionality
SELECT ABS(cast('2019-01-1' AS TIMESTAMP) - CURRENT_TIMESTAMP ) / (1000*60*60*24) LIMIT 100
this will get you a number of days between now and 1st of January
So far that's all what they have in their docs
You can subtract INTERVAL from TIMESTAMP, but before any matematichal operation you need to CAST the datatype, you can do it in this way:
SELECT now() - CAST('14 day' AS INTERVAL)
Or the same function of above, but in a contracted way
SELECT now() - '14 day'::INTERVAL;
As a string to be CAST to an INTERVAL you can use a number followed by any of this:
second
minute
hour
day
week
month
quarter
year

PostgresSQL - the date works weird

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;

How to find records from yesterdays time till todays time in sql?

I am trying to find records from yesterdays 10:30 PM till today's 10:30 PM with SQL query. Please help me with sql query to find such records.
Maybe its a duplicate question, if so please link me to that. Don't want any pl-sql function.
A simple way to do this is to subtract times and compare dates. So, one way is:
select t.*
from t
where trunc(datecol) = trunc(sysdate - 1.5/24);
It is more efficient to use a direct comparison (because Oracle can more readily use an index):
select t.*
from t
where datecol >= trunc(sysdate) - 1.5/24 and
datecol < trunc(sysdate) + 1 - 1.5/24;
Note: You can also use interval for this purpose, if you are less old-fashioned than I am:
select t.*
from t
where datecol >= trunc(sysdate) - interval '90' minute
datecol < trunc(sysdate) + interval '1' day - interval '90' minute;
You can get the yesterday date with SYSDATE - 1. You would need something like this:
SELECT ...
FROM ...
WHERE date_field BETWEEN SYSDATE-1 AND SYSDATE

How to list records with date from the last 10 days?

SELECT Table.date FROM Table WHERE date > current_date - 10;
Does this work on PostgreSQL?
Yes this does work in PostgreSQL (assuming the column "date" is of datatype date)
Why don't you just try it?
The standard ANSI SQL format would be:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10' day;
I prefer that format as it makes things easier to read (but it is the same as current_date - 10).
http://www.postgresql.org/docs/current/static/functions-datetime.html shows operators you can use for working with dates and times (and intervals).
So you want
SELECT "date"
FROM "Table"
WHERE "date" > (CURRENT_DATE - INTERVAL '10 days');
The operators/functions above are documented in detail:
CURRENT_DATE
INTERVAL '10 days'
My understanding from my testing (and the PostgreSQL dox) is that the quotes need to be done differently from the other answers, and should also include "day" like this:
SELECT Table.date
FROM Table
WHERE date > current_date - interval '10 day';
Demonstrated here (you should be able to run this on any Postgres db):
SELECT DISTINCT current_date,
current_date - interval '10' day,
current_date - interval '10 days'
FROM pg_language;
Result:
2013-03-01 2013-03-01 00:00:00 2013-02-19 00:00:00
The suggested answers already seem to solve the questions. But as an addition I am suggesting to use the NOW() function of PostgreSQL.
SELECT Table.date
FROM Table
WHERE date > now() - interval '10' day;
Additionally you can even specifiy the time zone which can be really handy.
NOW () AT TIME ZONE 'Europe/Paris'
Starting with Postgres 9.4 you can use the AGE function:
SELECT Table.date FROM Table WHERE AGE(Table.date) <= INTERVAL '10 day';
Just generalising the query if you want to work with any given date instead of current date:
SELECT Table.date
FROM Table
WHERE Table.date > '2020-01-01'::date - interval '10 day'
I would check datatypes.
current_date has "date" datatype, 10 is a number, and Table.date - you need to look at your table.
you can use between too:
SELECT Table.date
FROM Table
WHERE date between current_date and current_date - interval '10 day';