Changing the date used within a query which has been provided by a parameter - sql

I am trying to create a query which will be used within a reporting dashboard. the query needs to pull a date from a parameter which is feed from a datetime control within the dash board.
The issue i have is that the date time control will show the current date or can be changed using a drop down calender. I need the query to look at the date provided and perform a bit of an if statement.
If the date provided is the same month as the current month. I need the date used within the query to be the last day of the previous month. ELSE it should use the last day of the month selected.
I have tried several variations writing my code into the Where Clause but have so far not come to any success. Even just trying to get the query to give me the last day of the previous month (Code below) has not worked.
Any ideas would be welcome. I am really stuck on this.
SELECT A.ETLKEY, A.EFFECTIVEDATE, A.ACTUAL,
FROM DBO.View
WHERE (A.EFFECTIVEDATE = (DateAdd("d", -1, CDate(
"01/"+CStr(Format(&EndDate(),"MM/yyyy"))))))
Second attempt
DECLARE #dtDate DATE
SET #dtDate = &Enddate
--'Month/day/year'
SELECT Case
When #dtDate = '08/01/2016'
-- Last day of previous month, as this month will have no date in it
Then DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate),0))
-- Last day of month stated.
Else DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0))
So final working Code for anyone who might find this useful in the futrue
DECLARE #dtDate DATETime
SET #dtDate = &Enddate
--'Month/day/year'
SELECT Case
--compares month from parameter to todays month
When DATEPART(mm,#dtDate) = DATEPART(mm,GETDATE())
-- Last day of previous month, as this month will have no date in it
Then DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate),0))
-- Last day of month stated.
Else DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0))
END

Related

How to decipher complex DATEADD function from MS Access

I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...

How to write a variable within SQL using Dateadd and DateDiff for finding the last TWO days of the previous month

I am trying to write a variable using the dateadd and datediff that shows the last two days of previous month. One variable will be the second to last day of the previous month, the one I am having trouble with. The other will be the last day of the previous month, the one I was able to get. I am using SQL Server.
I've tried looking for it on Stack and I have only seen the last day of the previous month given and NOT the second to last day. I tried learning the dateadd and datediff, (which I still want to do).
This is what I tried so far:
Declare #CurrentMonth as date = '3/1/2019'
Declare #SecLastDayPrevMonth as date = DATEADD(MONTH, DATEDIFF(MONTH, 0, #currentmonth), -2)
Declare #LastDayPrevMonth as date = DATEADD(MONTH, DATEDIFF(MONTH, 0, #currentmonth), -1)
The results I am getting for the seclastdayPrevMonth is 2/28/2019. Instead I would want 2/27/2019
I am also getting 2/28/2019 for lastdayprevmonth which is what I want.
I am writing variables because the current month will change every month, and instead of having to update the other days I need within my query, I want to use variables so I am only updating the current month and everything else is flowing through.
And explanation as to why my dateadd/datediff is wrong and an explanation for why the correct dateadd/datediff is the way it is, will be very helpful
Why not refer to the last day when calculating the second last day? Also, your usage of DATEADD is very weird. The syntax is DATEADD(interval, increment, datetime)
Declare #recmonth as date = '3/1/2019'
Declare #LastDayPrevMonth as date = EOMONTH(DATEADD(MONTH, -1, #RecMonth))
Declare #SecLastDayPrevMonth as date = DATEADD(DAY, -1, #LastDayPrevMonth)
SELECT #SecLastDayPrevMonth, #LastDayPrevMonth
So we can calculate the last day of the previous month by subtracting one month from a date and then calling EOMONTH, which returns the last day of a given month. Then the second last day is just subtracting one day from that.
Yields:
SecLastDayPrevMonth LastDayPrevMonth
------------------- ----------------
2019-02-27 2019-02-28
As to "why", DATEDIFF() takes 3 arguments: datepart (string representation of a specific date part), startdate, enddate (both of which must be convertible to a date-ish object).
0 is essentially SQL's epoch, which, in this case is 1/1/1900. So the difference in months between 0 and 3/1/2019 is (119*12)+2 (+2 because we exclude March, since we aren't calculating a full month) = 1430 months difference.
Then, we are trying to add 2 months to our value. DATEADD() takes 3 arguments: datepart, number, date. But, in the example, you are adding 1430 months to whatever date -2 gets converted to (in this case, I believe it would be 12/30/1899, or 2 days before epoch). So, 1430 months after 12/30/1899 would be 2/30/2019, but February only has 28 days in 2019, so it returns 2/28/2019. In a Leap Year, it probably would return 2/29/2019.
To get your #LastDayPrevMonth and #SecLastDayPrevMonth with only DATEDIFF() and DATEADD(), you just need to change your calculations a little.
First thing you want to do is find the First Day of your Given Month. That can be done with DATEADD(month,DATEDIFF(month,0,#CurrentDate),0). We're essentially using the same thing we used above to calculate the number of months since epoch, but then we are adding those months back to epoch.
Now that we know the First Day of the Given Month, all we have to do is subtract days to get a day from the prior month.
So,
DECLARE #CurrentDate date = '2019-03-15' ; -- Changed it to something in the middle of the month.
DECLARE #FirstDayOfGivenMonth = DATEADD(month,DATEDIFF(month,0,#CurrentDate),0) ; -- 3/1/2019
DECLARE #LastDayOfPrevMonth date = DATEADD(day,-1,#FirstDayOfThisMonth) ; -- 2/28/2019
DECLARE #SecLastDayOfPrevMonth date = DATEADD(day, -2, #FirstDayOfThisMonth) ; -- 2/27/2019
SELECT #LastDayOfPrevMonth AS LDPM, #SecLastDayOfPrevMonth AS SLDPM ;
DECLARE #FourDaysLeftInPrevMonth date = DATEADD(day, -4, #FirstDayOfThisMonth) ; -- 2/25/2019
SELECT #FourDaysLeftInPrevMonth AS FourDaysLeftPrev ;
Granted, since SQL 2012, this can all be accomplished much easier with the EOM() function to get to the last day of a month. But if you only could use the two original functions from your original question, this would be one way to get to your needed values.

T-SQL: How to get all records prior to the first day of the current month?

Using SQL Server: I am trying to find all records prior to the first day of the current month and set this as a parameter in an SSRS report (so I can't use a static value).
So, I need all records prior to the first day of the each current month going forward in column CREATEDDATETIME ('yyyy-mm-dd').
I have seen a lot of threads on how to find records for a specific month and various other searches but none specifically related to the above. Interested to see if the EOMONTH function will be of use here.
Thanks for the help and advice.
Here is an expression to use EOMONTH() function with optional parameter -1.
Explanations:
DateAdd: add 1 day to expression
getdate is current date
EOMONTH is end day of a given month; however, if you put an optional integer -1, this would mean last month
Thus: first day of current month is add one day to end of day last month
SELECT DATEADD(DAY,1,EOMONTH(getdate(),-1));
Result: 2018-04-01
SO in your query:
select *
from table
where CREATEDDATETIME < DATEADD(DAY,1,EOMONTH(getdate(),-1));
I would use datefromparts():
select t.*
from t
where CREATEDDATETIME < datefromparts(year(getdate()), month(getdate()), 1);
There's already two other answers that work, but for completeness here is a third common technique:
SELECT *
FROM [table]
WHERE CREATEDDATETIME < dateadd(month, datediff(month,0, current_timestamp), 0)
You might also get answers suggesting you build the date using strings. Don't do that. It's both the least efficient and most error prone option you could use.
all three answers are great, how ever you may find that it will select all the data prior to the 1st day of the current month until the 1st Createdate. This could cause the report to take forever to run, Maybe building in a limitation to the code I would use something like this to build a report that gives details for last month only.
Select [columns]
from [source]
where [Createdate] between
/*First day of last Month*/
DATEADD(mm, DATEDIFF(mm, 0, Getdate())-1, 0 and
/*First day of this Month*/
dateadd(mm,datediff(mm,0,Getdate()),0)

Values over a time period

I would like to calculate a value in SQL that does the following:
if the user's inputted month parameter (I'm writing the SQL to aggregate information first then using SSRS) is 90 days or more past today (or the most recent data entry), then use the value for that month. Otherwise, if the month parameter is within 90 days of the report run date (or most recent data entry), use the most recent value that IS in fact 90 days past. For example, if I run the report in August and specify the month parameter as August, I will want the value from May. If I specify the month parameter as April, I want April's value since 90 days has gone by since then. Apologies if this isn't concise. I just would like some direction.
Cheers.
As suggested by #DanBracuk in the comments, you should probably take the parameter from SSRS and just update it based on the current date in your T-SQL code.
Say you have an SSRS parameter Parameters!Month.Value which gets passed to your stored procedure as #Month.
You can use GETDATE() and date functions like DATEDIFF() and DATEADD() to adjust #Month as required, e.g. in the stored procedure code something like:
select #Month = case when datediff(dd, #Month, getdate()) > 90
then #Month
else dateadd(dd, -90, #Month)
end
select *
from MyTable
where MyDate < #Month
Update the first statement to meet your requirements; they seem a bit ambiguous at this point - apologies if I haven't understood the issue completely.

I want to find first day and any other in a month in SQL query

I want to find first day month of month and also like 3rd day or 5th day ,15th day or any day of the month .So how to find through query.I know how to find first day and last day of month.Mainy I want find other days.
For those of you following along who may not know how to get the First Day of the month in SQL Server you can do so with something like this. This will also give you the 5th, 10th or whatever you need.
DECLARE #FirstDay DATETIME
SET #FirstDay = (DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) - 1, -1) + 1)
SELECT GETDATE() AS CurrentDay
, #FirstDay AS FirstDay
, DATEADD(d, 10, #FirstDay-1) AS TenthDay
The -1 after the #FirstDay in the DateAdd is because the DateAdd will add the numbers of days onto the firstday, which will give you the 11th in that example. Of course you could just add one less day to make it work without the -1 but I prefer including it. Suit yourself.
If you know how to find the first day of a month, you can add the 2-day, the 4-day or the 14-day interval to the first day of the month to get, respectively, the 3rd, the 5th or the 15th day of the month.
Similarly you can get any day of the month by simply adding the proper number of days.
Different RDBMSs may offer different syntax to achieve the goal. Assuming #MonthBeginning to be a date or datetime value representing the first day of a month, here's how you can get, for example, the 5th day of the same month in Microsoft SQL Server:
SELECT DATEADD(day, 4, #MonthBeginning) AS FifthDay
Again, it may not be the way you should do that if your RDBMS is not MS SQL Server.