SQL date ranges [duplicate] - sql

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.

Related

Trying to accommodate relative defined date, such as 5 days ago, into my fixed date condition in PostgreSQL

I'm trying to condition my WHERE clause to accommodate relatively defined dates into my date filter. I'm pretty confused what type I need to use, if it's CONVERT or TO_DATE function, or if I need to put a CASE WHEN statement into my code.
This is the code that I have written so far:
WHERE event_create_verified_user_success.created_at_utc_date
BETWEEN DATE '2021-11-29' AND DATE '2021-12-05'
And this is the condition of the activity I need to finish:
If the desired date-period is not set manually using fixed dates like from “2021-11-29”
to “2021-12-05”, how would you change the where-clause to consider all data from relative
defined dates: “consider messages created between 10 days and 5 days ago (inclusive)”
I've only started PostgreSQL yesterday and the last time I've handle SQL was probably 4 years ago so I'm pretty confused at how much SQL has changed since then.
Thank you so much for helping!
The basic syntax hasn't really changed in the last 4 years (or even 15 years).
You can use current_date to obtain "today's date". You can subtract days from that
where ... between current_date - 10 and current_date - 5
If created_at_utc_date is a timestamp (= date and time) rather than a date (=date without time) it's better to use a range query though:
where created_at_utc_date >= current_date - 10
and created_at_utc_date < current_date - 4
Note the < combined with the next day you want to compare with.

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.

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: