Get the data between previous 11 months till current month using SQL - sql

Help me with the below question.
I have ordermonth which is in BigInt format starting from 201801 till 202912.
I need to get the records where ordermonth from last 11 months till current month.
How to achieve this?
Thanks in advance.

You can use the YEAR and MONTH functions with a bit of arithmetic
WHERE ordermonth >= YEAR(DATEADD(month, -11, GETDATE())) * 100 + MONTH(DATEADD(month, -11, GETDATE()))
AND ordermonth <= YEAR(GETDATE()) * 100 + MONTH(GETDATE())
I would advise you to store ordermonth as an actual date. You could store either the beginning or the end of the month (you can get that with EOMONTH())

Related

SQL Query + How to look back at a specific date

I have a query that currently looks at receipts back 28 days from the date of purchase.
AND PAYMENT_DATE >= DATEADD(DD, -28, CONVERT(DATE, GETDATE()))
Now I this should go back to November 15 of the previous year, still using PAYMENT_DATE.
Is it possible to get help on how to accomplish this change?
Thank you.
Just subtract a year from todays date and build a new date using DATEFROMPARTS
AND PAYMENT_DATE >= DATEFROMPARTS(YEAR(GETDATE())-1, 11, 15);
Use the follwing code
datediff(dd, PAYMENT_DATE, '20201115') = 0
Another approach
AND CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, PAYMENT_DATE))) = '15/11/2020'

What is -1 and 0 doing in date add function?

dateadd(mm, DATEPART(MONTH, DATE) - 1, 0) + DATEPART(DAY, DATE) - 1
OUTPUT date is in the year for example 1990-12-02 00:00:00:000
Full query is below:
SELECT dateadd(yy, (
DATEPART(YEAR, GETDATE()) + (
CASE
WHEN DATEPART(MONTH, GP_DATE) > 10
THEN 0
ELSE 1
END
) - 1900
), 0) + dateadd(mm, DATEPART(MONTH, GP_DATE) - 1, 0) + DATEPART(DAY, GP_DATE) - 1 GP_DATE
from table
I am trying to convert this query into snowflake syntax and snowflake syntax dateadd function does not allow 1, 0.
In SQL Server, there are some rather ugly implicit conversions available between datetimes and integers.
0, when converted to a date, becomes 1900-01-01.
You're also allowed to do maths on dates. Adding or subtracting 1 adds or subtracts 1 day from the date. Putting these facts together, we have:
dateadd(yy, (
DATEPART(YEAR, GETDATE()) + (
CASE
WHEN DATEPART(MONTH, GP_DATE) > 10
THEN 0
ELSE 1
END
) - 1900
), 0)
Which is taking the current year, and subtracting 1900 from it (or 1899 if the month is less than 11, for whatever reason). We then take that number and add it back to the date 0 (which as stated above is 1900-01-01). The result is that we get the first of January of next year or this year, depending on the month of GP_DATE. Call this D1.
Moving on:
dateadd(mm, DATEPART(MONTH, GP_DATE) - 1, 0)
is taking the month of GP_DATE, subtracting 1 from it and adding that number of months to the date 0 (1900-01-01). The result is a the 1st of whichever month GP_DATE is in, but in 1900. Let's call this D2.
When we add D1 and D2 together, we approximately get a date of the 1st of whichever month GP_DATE is in, in either this year or next year. Note, however, that this goes wrong if the D1 year is a leap year, we get it wrong by a day for months after February.
Finally, we take DATEPART(DAY, GP_DATE) - 1, where we take the day of the month from GP_DATE, subtract 1, and add that on to our result so far. This should set the final date to be on the same day of the month as GP_DATE, except for the error mentioned above.
So, it appears that the code is trying to take GP_DATE and get the same date in either this year or next year, depending on how late in the year GP_DATE is. However, it also appears it was never tested with consideration for leap years.
A far more likely correct version of this query would be this instead:
SELECT
DATEADD(year,
DATEDIFF(year,GP_DATE,CURRENT_TIMESTAMP) +
CASE WHEN DATEPART(month,GP_DATE)>10 THEN 0 ELSE 1 END
,GP_DATE)

Is there is a way to get the month end date for every month

How to get the current month end date through db2 query.
I should not need to modify the query for every month
Every month the sql is executing so it need to automatically take care
In Db2 11.1 this could be a solution:
select next_month(current date) - 1 day from sysibm.sysdummy1
Next_Month will return the first day of the next month from the data provided.
SELECT LAST_DAY(CURRENT_DATE) AS LASTDAY
FROM SYSIBM.SYSDUMMY1;
The query is working
LASTDAY
2019-04-30
The old way would be
CURRENT DATE - (DAY(CURRENT DATE)) DAYS + 1 MONTH
To get the last day of the current month, use this:
SELECT DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))
This will format the date for you
SELECT CONVERT(VARCHAR(10), DATE, 12)
See this for more information regarding the '12'. 12 is the right code
Another solution is to use TIMESTAMP_FORMAT as such:
TIMESTAMP_FORMAT('190404', 'YYMMDD')

How I can fetch last two full years and current year records in SQL Server

I have following expression in my where clause:
DA.Access_Date >= DATEADD(YEAR, -2, GETDATE())
But it returns data till '2015-02-17' i.e. current year minus two.
I want data of two full years and current year
e.g. 2015-01-01 to till date. Any inputs on this will be appreciated.
Try this : Here DATEADD(yy, DATEDIFF(yy,0,getdate()) will give start month of the year
DA.Access_Date >= DATEADD(YEAR, -2, DATEADD(YY, DATEDIFF(YY,0,GETDATE()), 0))
With the help of YEAR scalar function
WHERE
YEAR(DA.Access_Date) in (YEAR(GETDATE()),YEAR(GETDATE())-1,YEAR(GETDATE())-2)
Your condition should be like below. DATEADD(YEAR,DATEDIFF(YEAR, 0, GETDATE())-2,0) this will returns first day of 2015 year.
DA.Access_Date >= DATEADD(YEAR,DATEDIFF(YEAR, 0, GETDATE())-2,0)
Just compare the year.
Try
YEAR(DA.Access_Date) >= (YEAR(GETDATE()) - 2)
You should try this in where condition.
Year(DA.Access_Date) >= Year(getdate()) - 2

Function to go back 2 years, first day of last month

I'm hoping to find a solution for this to automate a report I have. Basically what I'm trying to accomplish here is grabbing a date (first day of previous month, two years ago through last day of previous month current year).
So the date span if running this month would look like this: between 4/1/2013 and 3/31/2015
I have found code to get the date two years ago but I'm not able to also incorporate the month functions... Any help is very much appreciated!
For year I'm using this:
SELECT CONVERT(VARCHAR(25),DATEADD(year,-2,GETDATE()),101)
First day of previous month 2 years ago:
SELECT CONVERT(DATE,dateadd(day, -1, dateadd(day, 1 - day(GETDATE()), GETDATE())))
Last day of last month:
SELECT CONVERT(DATE,DATEADD(month, DATEDIFF(month, 0, DATEADD(year,-2,GETDATE())), 0))
Then just do whatever logic you need with them
Your where clause can look something like this:
where date >= cast(dateadd(year, -2,
dateadd(month, -1, getdate() - day(getdate()) + 1)
) as date) and
date < cast(getdate() - day(getdate()) + 1 as date)
This makes use of the handy convenience that subtracting/adding a number to a datetime is the same as adding a date. The start date says: get the first day of the month, then subtract one month, then subtract two years. This could have been done as dateadd(month, -25, . . .), but I think separating the logic is clearer.
This gives you two dates you are looking for:
SELECT
CAST((DATEADD(yy, -2, DATEADD(d, -1 * DATEPART(dd, getdate()) + 1 , GETDATE() ))) as date) as yourTwoYearsAgoDate,
CAST((DATEADD(d, -1 * DATEPART(dd, GETDATE()), GETDATE())) as date) as yourEndOfLastMonthDate
Given a reference date (e.g. "today"),
declare #today date = '23 April 2015'
The 1st of the month is computed by subtracting 1 less than the day number of the current month:
select first_of_current_month = dateadd(day,1-day(#today),#today)
The last day of the previous month is day 0 of the current month, so to get the last day of the previous month, just subtract the current day number:
select last_of_previous_month = dateadd(day,-day(#today),#today)
Moving two years back is easy:
select two_years_back = dateadd(year,-2, #today )
Putting it all together, this should do you:
declare #today date = '23 April 2015'
select *
first_day_of_current_month = dateadd(day,1-day(#today),#today),
last_day_of_previous_month = dateadd(day, -day(#today),#today) ,
date_from = dateadd(year,-2, dateadd(day,1-day(#today),#today) ) ,
date_thru = dateadd(day, -day(#today),#today)
yielding the expected results:
first_day_of_current_month: 2015-04-01
last_day_of_previous_month: 2015-03-31
date_from : 2013-04-01
date_thru : 2015-03-31
So you should be able to say something like this:
select *
from foo t
where t.transaction_date between dateadd(year,-2, dateadd(day,1-day(#today),#today) )
and dateadd(day, -day(#today),#today)
If you have to deal with datetime values rather than date, its easier to not use between and say something like this:
declare #today date = current_timestamp -- get the current date without a time component
select *
from foo t
where t.transaction_date >= dateadd(year,-2, dateadd(day,1-day(#today),#today) )
and t.transaction_date < dateadd(year, 0, dateadd(day, -day(#today),#today)
[superfluous addition of 0 years added for clarity]