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

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';

Related

trunc(add_months(sysdate,3),'Q')-1 equivalent expression in Postgres

can anybody convert this oracle expression trunc(add_months(sysdate,3),'Q')-1) to postgresql?
Basically this expression gives you the last day of the current quarter (provided that you remove the last closing parenthese, which otherwise is a syntax error).
In postgres, you could phrase this as:
date_trunc('quarter', current_date) + interval '3 months' - interval '1 day'
This generates a timestamp value, that you can cast if you want a date (this distinction does not exist in Oracle, where a date stores the time component as well).
The Postgres equivalent of your Oracle calculation can be seen below.
select date_trunc('quarter', current_date + interval '3 month') - interval '1 day'

Filter Data for 30 months using subquery with INTERVAL function in Teradata

I would like to filter out the data using a sub query in the interval function
Following is the query i use
SEL * FROM my_table WHERE MY_DATE < CURRENT_DATE- INTERVAL '30' MONTH;
The above query works, However i want to parameterize the period '30' using a sub query. Please suggest how to achieve this.
Thanks in Advance
Don't use interval calculations with year/month as it will fail, e.g. DATE '2016-12-31' + INTERVAL '30' MONTH results in 2019-06-31 (according to Standard SQL) which obviously doesn't exist.
SELECT *
FROM my_table
WHERE MY_DATE < ADD_MONTHS(CURRENT_DATE, (SELECT -col FROM tab));
If col is actually an INTERVAL you need to cast it to an INT.

BigQuery SQL WHERE Date Between Current Date and -15 Days

I am trying to code the following condition in the WHERE clause of SQL in BigQuery, but I am having difficulty with the syntax, specifically date math:
WHERE date_column between current_date() and current_date() - 15 days
This seems easy in MySQL, but I can't get it to work with BigQuery SQL.
Use DATE_SUB
select *
from TableA
where Date_Column between DATE_SUB(current_date(), INTERVAL 15 DAY) and current_date()
Remember, between needs the oldest date first
You should probably switch the two around - the syntax should be the following:
WHERE date_column BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()
This works for me.
WHERE DATE(date_column) BETWEEN DATE(DATE_ADD(CURRENT_DATE(), -15, 'DAY'))
AND CURRENT_DATE()

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

Common way to compare timestamp in Oracle, PostgreSQL and SQL Server

I am writing an SQL query which involves finding if timestamp falls in particular range of days.
I have written that in the PostgreSQL but it doesn't works in Oracle and SQL Server:
AND creation_date < (CURRENT_TIMESTAMP - interval '5 days')
AND creation_date >= (CURRENT_TIMESTAMP - interval '15 days')
Is there are common way to compare the timestamp across different databases?
I'm not a SQL Server expert but I know this works on Oracle and Postgres and I suspect it may work on MSSQL but have no way to test it ATM.
AND creation_date < (CURRENT_TIMESTAMP - interval '5' day)
AND creation_date >= (CURRENT_TIMESTAMP - interval '15' day)
Or if you are using the date type instead of timestamp, you could do this but I'm pretty sure it wouldn't work on MSSQL. And the DATE type is quite different between Oracle and Pg.
AND creation_date < CURRENT_DATE - 5
AND creation_date >= CURRENT_DATE - 15
As was noted in the comments for OMG Ponies, you can only add ints to Date types not timestamps. (Oracle silently casts the timestamp to date)
How to compare two timestamps in postgresql, the following returns true:
select to_timestamp('2010-01-01 10:10:85.123', 'YYYY-MM-dd HH:MI:SS.MS') <
to_timestamp('2012-01-01 10:10:85.123', 'YYYY-MM-dd HH:MI:SS.MS');
Returns true because the 2012 is after the 2010
I don't believe there is common syntax that'll work across all database engines. In SQL Server, you do it like this:
AND creation_date BETWEEN DateAdd(dd, -5, GetUtcDate()) AND DateAdd(dd, -15, GetUtcDate())
I'm not sure about Oracle...
Oracle (I have tested both of these solutions):
AND (creation_date BETWEEN sysdate-15 AND sysdate-6)
This syntax is also valid:
AND (creation_date BETWEEN current_timestamp - INTERVAL '15' DAY
AND current_timestamp - INTERVAL '6' DAY)
Note that SYSDATE and CURRENT_TIMESTAMP are synonymous (sort of - see comments).
Note that BETWEEN returns values inclusive of the bounds. Since you are looking for values which are >= date-15 but < date-5, you need to specify -15 to -6 when using BETWEEN. The answers using an upper bound of -5 with a BETWEEN expression are wrong.
select * from timing where (db_time < CURRENT_DATE) AND (db_time > (CURRENT_DATE - INTERVAL '1' DAY)) ;
Again this is specific for Oracle, assume intime is TIMESTAMP(3) data type, you can do this
select * from MYTABLE where intime >= to_timestamp('2014-10-01 7:00:56', 'YYYY-MM-dd HH24:MI:SS')