Compare a datetime field within 2 hours [duplicate] - sql

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)

Related

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

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

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.

How to do Query in Postgres SQL based on time [duplicate]

This question already has answers here:
How to add a variable number of hours to a date in PostgreSQL?
(5 answers)
Closed 8 years ago.
I would like to do a query in postgres which should bring count of records that updated previous one hour
For example 9AM-10AM records updated is 100 means I should get 100.
10AM-11AM records update is 200 means I should get 200(not 300).
Sample time stamp in updated column 2011-02-03 09:00:00
I have tried like this
select count(*) from customer where updated>=now()-1 and updated<now()
I know now()-1 will take yesterday. I dont know how to minus one hour from now
This should be doable using the date/time related functions and operations provided by Postgres:
where updated between
(date_trunc('hour',current_timestamp) - interval '1 hour')
and
(date_trunc('hour',current_timestamp))
Explanation:
date_trunc('hour',current_timestamp)
Gives the current time with minutes, seconds and milliseconds set to 0, and
- interval '1 hour'
subtracts one hour from a given timestamp.
Combine those to create the range you want ( between )

sql query where date between two times [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Specific Time Range Query in SQL Server
For example I have 3 rows:
Id Date
1 '2011-01-02 09:14:23.0000000'
2 '2011-02-15 10:15:47.0000000'
3 '2011-03-18 21:12:33.0000000'
And I whant to take only rows, where TIME between 09:00 and 11:00 and any date.
I need this in result set:
Id Date
1 '2011-01-02 09:14:23.0000000'
2 '2011-02-15 10:15:47.0000000'
Thanks
Here's one approach
SELECT id, date
FROM myTable
WHERE CONVERT(VARCHAR(8),Date,108) between '09:00:00' and '11:00:00'