Out of a set of data returned grouped by months, only the initial month is returning incorrect data - sql

I am trying to return data for the last 24 months as of the end of last month. Only the initial month returns incorrect data, while all the other months are correct. I believe the issue is in the strBeginDate = section of the code. Any ideas what would be causing partially returned data for the initial month? Thank you very much.
Sub GetStaticApprovalRates_Slide6()
Dim strBeginDate
Dim strEndDate
strEndDate = Sheets("Instructions").Range("EndDate").Value
strBeginDate = DateAdd("m", -23, strEndDate) + 1
Sheets("Slide6Data").Select

It's hard to say exactly what's wrong based only on what you posted. But I do see that you are calculating the start date based on the end date, by only subtracting months. There is no allowance for days. So you might be missing some of that first month by not allowing for the early days of that first month.
That is , if end date occurs mid-month, I think your algorithm would cause start date to be mid-month also. Perhaps missing days 1-x of that first month.

Related

SQL if last day of pervious month has value change the beginning date of next month to start from that day

Need help with this sql code, I would like to roll back start day of new month if last day of pervious month has values.
Thanks
I would suggest nesting EOMONTH() in an IF statement.
Not sure what table(s) you're working with, but general layout would be:
IF(MAX(date_column) = EOMONTH(MAX(date_column), MAX(date_column), new_date)
Reference for EOMONTH():
https://www.sqlservertutorial.net/sql-server-date-functions/sql-server-eomonth-function/

SQL datediff translation

I am not a SQL user, it happens that I need to know what this type of SQL code means, can you help me please translating it to me?
CASE
WHEN DATEDIFF(to_date(B.DT_CAREPACK_END),
LAST_DAY(date_sub(add_months(current_date, 3), 20))) > 0
CASE statements let you introduce some conditional logic.
A full CASE sttatement would have a syntax like:
CASE
WHEN some logic is true THEN x
WHEN some other logic is true THEN y
ELSE THEN z
END as column_title
In your example, it doesn't look like you've provided the full statement as their is no END keyword.
Basically, this logic is checking when the difference between two dates (date-x and date-y) is positive or not. DATE_DIFF looks at the different between a start date (date-x) and an end date (date-y).
If date-x, the start date, is before date-y, the end date, then the result is positive. If the start date is after the end date, then the result is negative.
date-x is a date representation of the column DT_CAREPACK_END
date-y is taking the current_date, adding on 3 months (e.g. 4th September becomes 4th December), is is then subtracting 20 units (presumably days) and then setting that date to the last date of that month.
So, imagine DT_CAREPACK_END (presumably a date when something ends) is in the future and is 2022-10-02.
The inner logic here will take the current date (2022-09-04) and add 3 months to that date, making it 2022-12-04. Then, we are subtracting 20 days which is 2022-11-14. Then, we find the last day in that month, which would be 2022-11-30.
Finally, we look at the difference between 2022-10-02 (start date) and 2022-11-30 (end date). If that is a positive number, then the logic is satisfied. In this case, 2nd October is before 30th November, resulting in a positive logic and therefore the case logic is satisfied.
If the DT_CAREPACK_END is before the current_date logic, then it would be negative.
*N.B. I thought that date_add, date_sub and date_diff functions needed an interval unit to be explicitly stated (e.g. INTERVAL 20 DAY). I'm guessing the default here is days but that's an assumption on my part. I'm working in good-faith that the code snip is syntatically correct. *
Resources:-
Add Months:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions004.htm
Date Sub:
https://docs.oracle.com/cd/E17952_01/mysql-5.7-en/date-and-time-functions.html#function_date-add
Last Day:
https://docs.oracle.com/cd/E17952_01/mysql-5.7-en/date-and-time-functions.html#function_last-day

How do I get the date of the last day of the previous month in the previous year in SQL?

I am writing a query with a where clause which needs to return the final day of the previous month in the previous year. So if I ran it today (18/06/2014), it would return 31/05/2013. If I ran it next month on 19/07/2014 it would return 30/06/2013 and so on.
I can use
select DATEADD(day,-1,DATEadd(MONTH,datediff(month,0,GETDATE()),0))
which returns the final date of last month but I can't work out how to adapt this to give me the date a year previously.
I can add - 365 to the end of the above code which does what I want but wouldn't account for leap years. Although I don't expect my reports to be in use for long enough for this to necessarily matter it would be good to find a solution that works nicely (and to think that they might be).
Any solutions greatly appreciated.
Just add another dateadd(YY, -1, {your code}) around your getdate() to roll the year back first, then look to previous month (per comments below).
select DATEADD(day,-1,DATEadd(MONTH,datediff(month,0,DATEADD(YY,-1,GETDATE())),0))

Find first and last day for previous calendar month in SQL Server Reporting Services (VB.Net)

I'm creating a report in MS SQL Server Reporting Services and need to set the default Start and End Date report parameters to be the first and last dates of the previous calendar month and need help.
The report is generated on the 2nd calendar day of the month and I need values for:
Previous Calendar Month
- first day
- last day
I've been working with DateAdd, but have not been successful at creating an Expression (in VB.NET as I understand it). I would really appreciate any help you can give me!
Randall, here are the VB expressions I found to work in SSRS to obtain the first and last days of any month, using the current month as a reference:
First day of last month:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
First day of this month:
=dateadd("m",0,dateserial(year(Today),month(Today),1))
First day of next month:
=dateadd("m",1,dateserial(year(Today),month(Today),1))
Last day of last month:
=dateadd("m",0,dateserial(year(Today),month(Today),0))
Last day of this month:
=dateadd("m",1,dateserial(year(Today),month(Today),0))
Last day of next month:
=dateadd("m",2,dateserial(year(Today),month(Today),0))
The MSDN documentation for the VisualBasic DateSerial(year,month,day) function explains that the function accepts values outside the expected range for the year, month, and day parameters. This allows you to specify useful date-relative values. For instance, a value of 0 for Day means "the last day of the preceding month". It makes sense: that's the day before day 1 of the current month.
These functions have been very helpful to me - especially in setting up subscription reports; however, I noticed when using the Last Day of Current Month function posted above, it works as long as the proceeding month has the same number of days as the current month. I have worked through and tested these modifications and hope they help other developers in the future:
Date Formulas:
Find the First Day of Previous Month:
DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1))
Find Last Day of Previous Month:
DateSerial(Year(Today()), Month(Today()), 0)
Find First Day of Current Month:
DateSerial(Year(Today()),Month(Today()),1)
Find Last Day of Current Month:
DateSerial(Year(Today()),Month(DateAdd("m", 1, Today())),0)
Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
Dim firstDayLastMonth As DateTime
Dim lastDayLastMonth As DateTime
firstDayLastMonth = thisMonth.AddMonths(-1)
lastDayLastMonth = thisMonth.AddDays(-1)
I'm not familiar with SSRS, but you can get the beginning and end of the previous month in VB.Net using the DateTime constructor, like this:
Dim prevMonth As DateTime = yourDate.AddMonths(-1)
Dim prevMonthStart As New DateTime(prevMonth.Year, prevMonth.Month, 1)
Dim prevMonthEnd As New DateTime(prevMonth.Year, prevMonth.Month, DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month))
(yourDate can be any DateTime object, such as DateTime.Today or #12/23/2003#)
in C#:
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1)
new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1)
I was having some difficulty translating actual VB.NET to the Expression subset that SSRS uses. You definitely inspired me though and this is what I came up with.
StartDate
=dateadd("d",0,dateserial(year(dateadd("d",-1,dateserial(year(Today),month(Today),1))),month(dateadd("d",-1,dateserial(year(Today),month(Today),1))),1))
End Date
=dateadd("d",0,dateserial(year(Today),month(Today),1))
I know it's a bit recursive for the StartDate (first day of last month). Is there anything I'm missing here? These are strictly date fields (i.e. no time), but I think this should capture leap year, etc.
How did I do?
I was looking for a simple answer to solve this myself. here is what I found
This will split the year and month, take one month off and get the first day.
firstDayInPreviousMonth = DateSerial(Year(dtmDate), Month(dtmDate) - 1, 1)
Gets the first day of the previous month from the current
lastDayInPreviousMonth = DateSerial(Year(dtmDate), Month(dtmDate), 0)
More details can be found at:
http://msdn.microsoft.com/en-us/library/aa227522%28v=vs.60%29.aspx
This one will give you date no time:
=FormatDateTime(DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1)),
DateFormat.ShortDate)
This one will give you datetime:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
Dim aDate As DateTime = #3/1/2008# 'sample date
Dim StartDate As DateTime = aDate.AddMonths(-1).AddDays(-(aDate.Day - 1))
Dim EndDate As DateTime = StartDate.AddDays(DateTime.DaysInMonth(StartDate.Year, StartDate.Month) - 1)
'to access just the date portion
' StartDate.Date
' EndDate.Date

Calculating Months

I have an application where the user selects the dates of a first statement and a last statement. Example, first statement = 1/1/08, last statement = 12/1/08, should equal 12 statements.
However, when using the following code, the result is 11:
numPayments = DateDiff(DateInterval.Month, CDate(.FeeStartDate), CDate(.FeeEndDate))
Is there another way to calculate this, or do I have to be stuck with adding 1 to the result?
Add 1, as you write. ;)
The difference between 1/1/2008 and 12/1/2008 is 11 months. No changing that. ;)
Yes, you'd always have to add one though you may be able to add one to the end date or subtract one from the start date to also get this effect. Consider the case where the start and end dates are the same. Their difference is 0 but you'd still want 1 statement to show just to note one odd case.
Well, the number of months between Jan 1st and Dec 1st is 11... what you're looking for is the difference of months +1. So just add one :)
Also, the DateDiff function you're using is a VB6 hold-over. Better to express it like this:
numPayments = (Date.Parse(.FeeEndDate) - Date.Parse(.FeeStartDate)).TotalMonths + 1
You could try this one. Hope this is very helpful.
Dim myDate As Date
Dim dateNow As Date
Dim nextMonth As Date
myDate = Now
dateNow = Format(myDate, "MM/dd/yyyy")
nextMonth = DateAdd(DateInterval.Month, 5, dateNow) 'compute the next 5 months from date now. Let say, #12/6/2012# the result will be #5/6/2013#
MessageBox.Show(DateDiff(DateInterval.Month, dateNow, nextMonth) & "months==> " & nextMonth)
'This will count the number of months interval. The result will be 5 months=>> #5/6/2013 because we count december to may.