SQL query for getting data for last 3 months - sql

How can you get today's date and convert it to 01/mm /yyyy format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy.

SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())
Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.
or you can check against last 90 days.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())

Latest Versions of mysql don't support DATEADD instead use the syntax
DATE_ADD(date,INTERVAL expr type)
To get the last 3 months data use,
DATE_ADD(NOW(),INTERVAL -90 DAY)
DATE_ADD(NOW(), INTERVAL -3 MONTH)

I'd use datediff, and not care about format conversions:
SELECT *
FROM mytable
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3

Last 3 months
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(mm,-3,GETDATE())),0)
Today
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)

Last 3 months record
SELECT *, DATEDIFF(NOW(),`created_at`)as Datediff from `transactions` as t having Datediff <= 90

Related

SQL Athena getting the data for the past 12 months

I need to grab the data for the past 12 months. I am using SQL within AWS Athena. Below is my code:
CREATE
OR REPLACE VIEW response_view AS
SELECT
"cust"."customer_id",
"cust"."event_triggered_date"
FROM
(
db.population_view pop
INNER JOIN new_db.manual_response_view cust ON ("pop"."customer_id" = "cust"."customer_id")
)
WHERE "cust"."event_triggered_date" > current_date - interval '12' month
Gives me an error: cannot be applied to varchar, date
event_triggered_filed is a string
This is the structure of the date field: 2019-12-04 00:00:00.000
Try this.
CAST(EVENT_TRIGGERED_DATE AS DATE)
OR
CAST(EVENT_TRIGGERED_DATE AS TIMESTAMP )
Data Types
I had same problem I need to get the stating day of the 6th and 12th month from current date.
I have written this, it works for me I hope it will work for other as well.
please refer attached image for query result
select current_date as today, date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Start_of_the_date_6_month_before
For Staring day of the 12 month before date.
select current_date as today, date_add('month', -12, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Starting_day_of_the_month_12_before
Finally i make this changes like this :
With
six_month_And_minus_1_day AS (
select date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Start_of_the_date_6_month_before,
date_add('day', -1, date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01'))))) as Start_of_the_date_6_month_before_minus_1
)
select * from six_month_And_minus_1_day
Any one has better way to do this please suggest.

How to find the given any date (ex. 2018-11-05 00:30:27.863) is greater than 24 hours or it exceeded 24 hours from that date in SQL SERVER?

I want to send or trigger a mail where, given specified date exceeded 24 hour.like it should send please take any action after 24 hours in stored procedure.
DATEADD(day, -1,Getdate()) or (now() +interval 1 day)
These I have used but not able to see the expected output
I have tried
select * from table where date >= DATEADD(day, -1,Getdate())
where date ex. 2018-11-05 00:30:27.863
Your query needs the < symbol instead of >:
select * from table where date <= DATEADD(day, -1, Getdate())
because you need date to be prior than Getdate() - 1 day.
Or:
select * from table where Getdate() >= DATEADD(day, 1, date)
You can use DATEDIFF() function like below -
select * from table where DATEDIFF(hh,dateColumn,GETDATE()) > 24

SQL Select Records based on current date minus two days

I have an orders table which contains an order ID, order date and order description.
I want to run a select query which captures all orders that have been created in the last two days. so the current date minus two days. from the 14th December, I would want to select all orders where the order date is > 13th December. This needs to use a Get date function to pick up the current date and minus the days.
I have tried:
select * from orders where orderdate > getdate() - 2
but this is not producing the correct results. Any idea's how to do this please?
you should try to use dateadd function
select * from orders where orderdate > dateadd(dd,-1,cast(getdate() as date))
Now, this may be exactly what you need but then you need to understand that by casting to date we remove the time part and effectively go back to the start of the day and a day behind it(-1) gives the start of yesterday.
Try this:
select * from orders where orderdate > cast(getdate() - 1 as date)
If you want the orders of the last two days, use DATEADD to add days to today's date (in your case -2 days) then use DATEDIFF to compare the two days:
SELECT * FROM orders
WHERE DATEDIFF(DAY, DATEADD(DAY, -2, GETDATE()), orderdate) > 0
Now, assuming all orders have dates in the past and none in the future (which is what it should be), you can simply use DATEDIFF like this:
SELECT * FROM orders
WHERE DATEDIFF(DAY, orderdate, GETDATE()) <= 2
Note: you can use < 3 instead of <= 2.
Looks like dateadd and convert will solve the problem.
select o.*
from orders o
where o.orderdate >= dateadd(day, -2, convert(date, getdate()))

SQL Server 3 month from now

I need a SQL select statement to retrieve all employees that their enddate contract will end three month from now and only three month not more or less
It depends what you mean by 3 months from now. If you mean on that exact date, then:
WHERE my_date_column = cast(dateadd(month, 3, getdate()) as date)
Note the cast to date to remove the time component. This is ambiguous and under some circumstances might miss employees or count them twice.
If you want employees whose contract ends in the 3rd month from today, then use:
WHERE DATEDIFF(month, getdate(), my_date_column) = 3
So, if this is January, this will return employees whose contract ends any time in April.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, +90, GETDATE())
OR
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, +3, GETDATE())
OR
SELECT *
FROM TABLE_NAME
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3

Comparing dates in SQL Server

I have a DateTime column named EXP_Date which contains date like this :
2014-07-13 00:00:00.000
I want to compare them, like this query :
SELECT COUNT(*)
FROM DB
WHERE ('2014-07-15' - EXP_DATE) > 1
I expect to see the number of customers who have their services expired for over a month.
I know this query wouldn't give me the correct answer, the best way was if I separate the Year / Month / Day into three columns, but isn't any other way to compare them as they are?
You can use DATEADD
SELECT COUNT(*)
FROM DB
where EXP_DATE < DATEADD(month, -1, GETDATE())
Try this
SELECT COUNT(*)
FROM DB
where DATEADD(month, -1, GETDATE()) > EXP_DATE
SELECT COUNT(EXPIRE)FROM
(Select CASE WHEN EXP_DATE < DATEADD(month, -1, GETDATE())THEN 1 ELSE 0 END)AS EXPIRE FROM DB
)tt
Another way using DATEDIFF
SET DATEFORMAT DMY --I like to use "dateformat"
SELECT COUNT(*)
FROM DB
WHERE (DATEDIFF(DAY,#EXP_DATE,GETDATE())) >= 30 --Remember, instead "Day" you can use week, month, year, etc
Syntax: DATEDIFF ( datepart , startdate , enddate )
Depart: year, quarter, month, day, week...
For more information you can visit MSDN