How to use where < current_date sql? - sql

On our PostgresSQL Database we would like to run a query.
Select (*)
Where
domain.blabla.date > 'yesterday'
AND
domain.blabla.date < 'current_date'
The goal is to get only yesterday's data.
The error we get is the following:
[Err] ERROR: date/time value "current" is no longer supported
Do you see a way to solve it?
Thank you very much!
Luca

For The goal is to get only yesterday's data:
Select (*)
from domain.blabla
Where date_trunc('day', date) = date_trunc('day', current_date - 1);

This query will fetch ONLY yesterday's value
select * from table_name where day > current_date- interval '1 day'
OR using yesterday
select * from table_name where day > current_date 'yesterday'
SQLFiddle
Refer to PostgreSQL Date/Time Documentation for more on this
P.S - You don't have to put the function in single quotes ' '

For the current date in SQL you can use
SELECT CONVERT(date, getdate())
For yesterday date in SQL you can use
SELECT dateadd(day,datediff(day,1,GETDATE()),0)

Related

sql search in a date column from current date to current date+7 not working

This query works and brings up proper results:
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= '2020-12-17';
This query brings up no results even though there are dates within a week of now:
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= current_date + 7;
Can someone please tell me what is wrong?
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= date_add(current_date, INTERVAL 7 DAY);
is what solved it. subodh led me in the right direction
Try
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= DATEADD(dd, 7, current_date );
More info DATEADD
You may verify if the data type for both the 'current_date' and 'start' column is either of Date, DateTime,  DateTime2, or SmallDatetime.
If you just select the results, you can se what is happening:
current_date current_date + 7
2020-11-29 20201136
Hmmm, Nov. 36? What date is that? Oh! What is happening is that the database is converting the "date" to an integer and just adding 7.
The simplest solution is to use interval arithmetic:
current_date + interval 7 day
In your code, that looks like:
WHERE start >= current_date AND
start <= current_date + INTERVAL 7 DAY

PostgreSQL - Select Query between today's start time and end time

I need to query database like - SELECT FROM table_name WHERE created_at = <--BETWEEN TODAY'S START AND END DATETIME-->
Sample data from created_at column is - 2020-09-28 17:02:14
Tried method is as follows but it didn't work,
SELECT FROM users WHERE created_at=CURRENT_TIMESTAMP;
SELECT FROM users WHERE created_at=CURRENT_DATE;
Can any one help me with this query
Is this what you want?
where created_at >= current_date
and created_at < current_date + interval '1 day'
This gives you all rows whose created_at belongs to the current day.

Select rows where day is less than today

How do I select rows in the past starting from yesterday in Oracle DB where a field like created_date is a timestamp(6)?
I don't want to compare time, just date.
If you want exactly one day prior to the current time:
select *
from table t
where created_date < sysdate - 1;
If you want times before today:
select *
from table t
where created_date <= trunc(sysdate);
From the Oracle documentation on SELECT :
SELECT * FROM orders
WHERE created_date < TO_DATE('2014-04-28', 'YYYY-MM-DD');
I can pass this date format from my application, worked like a charm.
As you want to compare just date:
select *
from table t
where date(created_date) < DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
you can use cast function to deal with timestamp as date:
SELECT cast(SYSTIMESTAMP(6) as date)
FROM dual;
so you can select rows with "yesterdate" date by:
select ....
where cast(SYSTIMESTAMP(6) as date) like sysdate - 1
note: replace SYSTIMESTAMP(6) with column name which has timestamp type.

SQL date check within set number of days

Im trying to find out how many clients viewed a property within 14 days of May 20, 2004, either before or after. Not really sure at all how to go about this.
Im assuming i need to group it and use a having?
EDIT: I am using oracle now
select count(*)
from VIEWING
WHERE CLAUSE?
For a one time query with that specific date,
select count(*) clients
from yourtable
where yourdatefield >= {d'2004-05-06'}
and yourdatefield < {d'2004-06-08'}
You might want to consult a calendar to see if those dates are correct.
Edit #1, since you are using Oracle, you can use:
select count(*) TotalClients
from yourtable
where dt >= (to_date('2004-05-20', 'yyyy-mm-dd') - INTERVAL '14' DAY)
and dt <= (to_date('2004-05-20', 'yyyy-mm-dd') + INTERVAL '14' DAY)
See SQL Fiddle with Demo
Based on some of your previous questions you were using MySQL.
If you are using MySQL then you can use the DATE_ADD() function to get the date range and then use count(*) to return all records from those dates:
select count(*) TotalClients
from yourtable
where dt >= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL -14 day)
and dt <= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL 14 day)

How to select records from last 24 hours using SQL?

I am looking for a where clause that can be used to retrieve records for the last 24 hours?
In MySQL:
SELECT *
FROM mytable
WHERE record_date >= NOW() - INTERVAL 1 DAY
In SQL Server:
SELECT *
FROM mytable
WHERE record_date >= DATEADD(day, -1, GETDATE())
In Oracle:
SELECT *
FROM mytable
WHERE record_date >= SYSDATE - 1
In PostgreSQL:
SELECT *
FROM mytable
WHERE record_date >= NOW() - '1 day'::INTERVAL
In Redshift:
SELECT *
FROM mytable
WHERE record_date >= GETDATE() - '1 day'::INTERVAL
In SQLite:
SELECT *
FROM mytable
WHERE record_date >= datetime('now','-1 day')
In MS Access:
SELECT *
FROM mytable
WHERE record_date >= (Now - 1)
SELECT *
FROM table_name
WHERE table_name.the_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
MySQL :
SELECT *
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
The INTERVAL can be in YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
For example, In the last 10 minutes
SELECT *
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)
Which SQL was not specified, SQL 2005 / 2008
SELECT yourfields from yourTable WHERE yourfieldWithDate > dateadd(dd,-1,getdate())
If you are on the 2008 increased accuracy date types, then use the new sysdatetime() function instead, equally if using UTC times internally swap to the UTC calls.
in postgres, assuming your field type is a timestamp:
select * from table where date_field > (now() - interval '24 hour');
If the timestamp considered is a UNIX timestamp
You need to first convert UNIX timestamp (e.g 1462567865) to mysql timestamp or data
SELECT * FROM `orders` WHERE FROM_UNIXTIME(order_ts) > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
select ...
from ...
where YourDateColumn >= getdate()-1
SELECT *
FROM tableName
WHERE datecolumn >= dateadd(hour,-24,getdate())
Hello i now it past a lot of time from the original post but i got a similar problem and i want to share.
I got a datetime field with this format YYYY-MM-DD hh:mm:ss, and i want to access a whole day, so here is my solution.
The function DATE(), in MySQL: Extract the date part of a date or datetime expression.
SELECT * FROM `your_table` WHERE DATE(`your_datatime_field`)='2017-10-09'
with this i get all the row register in this day.
I hope its help anyone.
In SQL Server (For last 24 hours):
SELECT *
FROM mytable
WHERE order_date > DateAdd(DAY, -1, GETDATE()) and order_date<=GETDATE()
In Oracle (For last 24 hours):
SELECT *
FROM my_table
WHERE date_column >= SYSDATE - 24/24
In case, for any reason, you have rows with future dates, you can use between, like this:
SELECT *
FROM my_table
WHERE date_column BETWEEN (SYSDATE - 24/24) AND SYSDATE