Getting the last month date's data in sql - sql

I have the below query to get the last month date's data. Suppose if today is 23rd Dec, then it fetches 23rd Nov data. But when it's 31st Dec, then there would be no 31st Nov. In that case it should fetch 30th Nov data.
So, I want to decrease one day and check the condition again and should fetch the data. In this scenario, suppose on March 31st, first that query even should check for feb 31st, then it should check for feb 30, then 29th and so on until the valid date of that particular month.
My current query:
select *
from dataTable
where date(datecolumn)=date(add_months(DATE(sysdate-1) ,-1));
So, this only fetches last month's date data. So, can someone please suggest me how to check for the mentioned validation in the query and fetch the data?

try this:
SELECT dateadd(mm, -1, Convert(Datetime, '2013-12-31')

Related

not getting previous month record using datepart in sybase

Using below depart syntax to fetch for previous month record, it is working fine till previous year,however it is giving 0 value in January month.How can we get pervious month with date part even if year is change ?
DATEPART(month(GETDATE()) -1
I understand that I used another type of DB, but I want to give a hint. I am using sql server 2019.
Firstly, you need to substitute date and only then take datepart from it.
Queries:
--dateadd -1 would subtract 1 from current month
--(Jan - 1 2022), would be December 2021
select datepart(month, dateadd(month, -1, getdate()))
--also date add covers internally the problem with 30,31 days.
--May always has 31 days, April 30. So -1 subtraction from 31th of May,would result in 30th of April.
select dateadd(month, -1, cast('2021-05-31 10:00:00' as datetime))

Weeks rolling up to months in SQL (date_trunc)

I am working on a MODE Case Study which can be accessed here https://mode.com/sql-tutorial/a-drop-in-user-engagement/#the-problem.
I am trying to access Table 2: Events which has a date column 'occurred_at'. I wanted to check the time frame of this case study, that is weeks and months.
I wrote a simple query
select distinct(date_trunc('week', occurred_at)) as week, date_trunc('month', occurred_at) as month
from tutorial.yammer_events
where event_type = 'engagement'
order by week;
and to my surprise, the first week of '2014-04-28' showed the month 'May' instead of 'April.
Can someone please tell me what is the reason for this?
Thank you
The PostgreSQL date_trunc rolls up the date to the first instance of the date depending upon the granularity (day, week, month, etc.)
For month this instance is the first day of month i.e. Day 1.
For week this instance is the first day of week i.e. Monday.
Suppose the date is 4th July 2021 which is Sunday, then the date_trunc will result in 1st July 2021 for month and 28th June 2021 for week which is Monday inside that week.
Suppose the date is 5th July 2021 which is Monday itself, then the date_trunc will still result in 1st July 2021 for month but result in 5th July 2021 for week since it is already Monday.

Snowflake retrieving data of past month on a particular date of current month

I am new to snowflake and my manager wants me to retrieve the data of the past month when it is 5th of the current month. For example if today is 5th April, then ask snowflake to retrieve the data of the past month i.e. from 1st March 2021 to 31st March 2021 and similar for all the other months.
The reason why he wants to update the last month data on 5th of every next month because that is the day when we get the data.
I tried to use the DATEADD function but it is more complicated than just using this function.
Thanks in advance!
PS: The data for every month has same date. for example: the date is like - April 20th will be stored in the database as "2021-4-01" - and same for April 25th date will be stored as "2021-4-01" .
The day doesn't change in the database, just the month and year.
as to the prior month window that can be done via DATE_TRUNC and DATEADD
select
current_date as cd
,date_trunc('month', cd) as end_range
,dateadd('month', -1, end_range) as start_range
;
gives:
CD END_RANGE START_RANGE
2021-04-21 2021-04-01 2021-03-01
the other half of the question only do it on the 5th, if you have a task run daily etc. can be solved via
,day(current_date) = 5 as is_the_fifth
or if in an inline way
iff(day(current_date) = 5, <do true stuff>, <do false stuff>)

Convert date to first of current month if date is previous to current month

I have a query with an IIF() expression in one column that I am using to identify if a date is previous to the current month to then amend it if so. So if I run the query on 19th March 2014 and the EffFrom date is before 1st March 2014, I would want that column entry to now appear as 1st March 2014.
I am using the below expression which is pretty much doing what I want, however I know it is not considering the year -- i.e. it is changing an entry of 1st Jan 2015 to be 1st March 2014.
EffFrom:
IIf(Month([Table.Efffrom])"Less than symbol"Month(Date()),Date()-Day(Date())+1,[Table.Efffrom])
Can someone correct the expression for me?
I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):
id Efffrom adjusted_date
1 1/1/2014 3/1/2014
2 3/1/2014 3/1/2014
3 3/31/2014 3/31/2014
4 1/1/2015 1/1/2015
If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.
SELECT
y.id,
y.Efffrom,
IIf
(
y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
DateSerial(Year(Date()), Month(Date()), 1),
y.Efffrom
) AS adjusted_date
FROM YourTable AS y;
Here's what i do to get the first day of the next month
EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))

SQL Between Two Dates Missing End Date (SSRS)

I'm using SSRS 2008r2 to generate reports. Using following WHERE statement in SQL query
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate <= #Created_Parameter2)
I'm using parameters with the data type of DateTime. Users then select day from a calendar. I want to get results where jobs have been created between date 1 (#Created_Parameter) AND date 2 (#Created_Parameter2) INCLUSIVE.
But results being returned do not include the second date (#Created_Parameter2). If I select 01/07/2013 - 05/07/2013 I get 01, 02, 03, 04 but no 05. If I select 01/07/2013 - 06/07/2013 I get 01, 02, 03, 04, 05.
I've tried using:
WHERE (NonPMJobs.npmJobCreateDate BETWEEN #Created_Parameter AND #Created_Parameter2)
but get same results.
What am I missing here and why isn't WHERE statement inclusive? Any pointers would be very much appreciated.
Well, you need to think about this: a DATETIME like this: 05/07/2013 means the 5th of July (or 7th of May - depending on your locale) at midnight when the day starts (a 00:00 hours, so to speak).
So this does NOT include the events that happen on that day!
The best solution would be to use
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate < DATEADD(DAY, 1, #Created_Parameter2))
and basically getting everything that's smaller than the next day based on #Created_Parameter2. So for your 5th of July, that would be anything before the 6th of July - which includes all events from the 5th of July.