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

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.

Related

Previous Workday in Where Clause [duplicate]

This question already has answers here:
How to get Previous business day in a week with that of current Business Day using sql server
(8 answers)
Select the previous business day by using SQL
(3 answers)
Closed 3 months ago.
How do I obtain the previous working date in a where clause
without doing the following and changing this manually
WHERE date = GETDATE()-1
The date column is datetime but I just need the date too.

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

Getting the last day of an already existing date in SQL Server Management Studio [duplicate]

This question already has answers here:
SQL Query to find the last day of the month
(15 answers)
Closed 5 years ago.
I have a table with a list of clients, with a column that shows the date their accounts were created. I want to make the last day of this date. I tried using the EOMONTH function, but it does not work.
For instance, if client 1 came on January 6th, and client 2 came on February 6th of this year, I want it to show 31-01-2018 and 28-02-2018.
Any ideas?
Thanks.
Gordon is right, I have tried EOMONTH for your given data and it returns perfect value look at below, in second parameter you have to pass value as per requirement if you pass value as 1 it will give you last date of next month i.e Jan+1=Feb
DECLARE #myDate Datetime='06-Jan-2018'
SELECT EOMONTH(#myDate,0) AS MyDate
If your version not support the EOMONTH function then try below query:
SELECT CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#myDate)+1,0)) AS DATE)
Output:

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 date ranges [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
90 days range using SQL server
I am trying to get the counts 90 days prior to the operational date and the counts 90 days after the operational date. For example, my operational date is 4/1/2004. So,90 days prior to 4/1/2004 is (1/2/2004 to 3/31/2004) and 90 days after (including 4/1/2004) is 6/29/2004.
I used the following scripts and mannually calculate the days, which is not efficient...
select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site
select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
You should look at the DATEDIFF function or equivalent if you're not using SQL Server.
DATEDIFF on MSDN
If you are passing the date parameter in from an application, consider modifying the application to do the date range calculation for you. By passing in two parameters, you are taking the burden of the calculation off SQL, which will improve performance.