Oracle select statement where date is greater than 30 days [duplicate] - sql

This question already has an answer here:
Oracle SQL Where clause to find date records older than 30 days
(1 answer)
Closed 2 years ago.
I want to get an ORACLE statement which returns rows that are older than 30 days from the the date of creation.
My table has a field "date_entered" that contains the date and the time it was inserted in the database .
Thanks,

your select statement
WHERE date_entered < TRUNC(SYSDATE)-30

Related

Number of days between two columns SQL [duplicate]

This question already has answers here:
Postgresql difference between two columns dates?
(3 answers)
Get month,days difference between two date columns
(1 answer)
Date column arithmetic in PostgreSQL query
(1 answer)
how to calculate only days between two dates in postgres sql query .
(2 answers)
Closed 4 years ago.
I was looking for the answer to this question, and I found here that DATEDIFF() function is a way to do it when you have the dates. But, what if you don't have specific dates?
What if you want to find the interval of time between the date in 2 columns (rental_date, return_date).
I tried to use DATEDIFF() function, but it looks like you need the actual date.
You can use date_part for finding differences between two dates in the PostgreSQL.
The syntax is like as follow.
DATE_PART('day', enddate - startdate);
You can get years, months, weeks, hours, minutes seconds as well. You can check more about the usage of date different and date part function in PostgreSQL by clicking on this link.
if your two column data type is date then you could do subtraction
select '2018-12-10'::date - '2018-11-18'::date
so in your case it would be
with t1 as
(
select '2018-12-10'::date as rental_date, '2018-11-18'::date as return_date
)
select rental_date-return_date from t1

SQL Server - Select all records whose dates are within 5 days of target date [duplicate]

This question already has an answer here:
SQL Server Management Studio make default date 5 days from now
(1 answer)
Closed 4 years ago.
I need to create a view everyday with all records reaching the "Prazo" date ('Prazo' stands for due date in Portuguese), so everyday I need to make a select showing all the records that are 5 days from reaching the due date. How can I do that?
You can do:
Select *
from Table as t
Where DATEDIFF(DD, GETDATE(), PrazoDate) = 5
It just says how many records from today are 5 days from reaching their due date.

Compare a datetime field within 2 hours [duplicate]

This question already has answers here:
SQL: Get records created in time range for specific dates
(3 answers)
Closed 8 years ago.
I have a table with the field ENTERED_ON (with both date and time value). I want to write a query that return records that have ENTERED_ON value that is past 2 hours comparing to current date time.
For example, if entered_on is 2014-05-06 11:00AM, and currently it's 2014-05-06 2:00PM, I would like to return all records that past the 2 hours when comparing to current date time.
You can write query using INTERVAL. It will looks like
SELECT * FROM Table WHERE `ENTERED_ON` > DATE_ADD(NOW(), INTERVAL -2 HOUR)

SQL last month date of given date [duplicate]

This question already has answers here:
Get the last day of the month in SQL
(22 answers)
Closed 9 years ago.
i am using sql
How to get the last day of the month for a given date
01/03/2014 should return 01/31/2014
I only need the date part.. Thanks
For SQL-SERVER, try this:
SELECT convert(varchar,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'01/03/2014 ')+1,0))),101)

SQL Calculate week number from a date column [duplicate]

This question already has answers here:
How to extract week number in sql
(4 answers)
Closed 9 years ago.
I have the following database schema
Facts(ReportNo, ReportName, Load_Date)
Load_Date is a column of (number type) and has
records of date in YYYYMMDD Format.
I need to print the week number for each row using the already existing dates
in Load_Date. Since it is already of type number, I do not have to convert it from date to char.
Output should be
**Week Number | ReportNo | ReportName
try
select ReportNo,ReportName,to_char(to_date(Load_Date,'yyyymmdd'),'ww') from Facts