T-SQL to get dates - sql

I've SSRS sales report to which I need to pass the dates for previous month's start date and end date which I am able to pass using below code. However, since the sales report has data from the past year(2014) I need to pass the dates for last year as well. The below code gives StartDate1 as 2015-02-01 and EndDate1 as 2015-02-28. I need to get the dates for past year like 2014-02-01 as StartDate2 and 2014-02-28 as EndDate2
SELECT DATEADD(MONTH, DATEDIFF(MONTH, '19000201', GETDATE()), '19000101') AS StartDate1,
DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '18991231') AS EndDate1

Since last day of month can vary, the important thing is to get the first day of the current month this year. From that you can calculate the other three values.
You could do this easily with expressions in the parameters' default values instead.
Start of Month
=Today.AddDays(1-Today.Day)
End of Month
=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
Start of Month Last Year
=Today.AddDays(1-Today.Day).AddYears(-1)
End of Month Last Year:
=Today.AddDays(1-Today.Day).AddYears(-1).AddMonths(1).AddDays(-1)
But if you really want to do it in SQL, you can. Note below I'm describing how to get the start of month and then just using placeholder variables for it in the other expressions for clarity.
--Start of Month
dateadd(month, datediff(month, 0, getdate()), 0)
--End of Month
dateadd(day, -1, dateadd(month, 1, #StartOfMonth))
--Start of Month Last Year
dateadd(year, -1, #StartOfMonth)
--End of Month Last Year
dateadd(day, -1, dateadd(month, 1, #StartOfMonthLastYear))
Those are the pieces. Put them together into one giant, very hard to read select statement like so:
select
dateadd(month, datediff(month, 0, getdate()), 0) StartDate1,
dateadd(day, -1, dateadd(month, 1, dateadd(month, datediff(month, 0, getdate()), 0))) EndDate1,
dateadd(year, -1, dateadd(month, datediff(month, 0, getdate()), 0)) StartDate2,
dateadd(day, -1, dateadd(month, 1, dateadd(year, -1, dateadd(month, datediff(month, 0, getdate()), 0)))) EndDate2
Now you see why I recommend just using internal parameters that can leverage the .NET datetime class methods.

Related

Two Seperate WHERE clauses in a DELETE Function

I want to perform a DELETION on certain Transactions that were made between the first day of the current month and the last day of the month. The deletion process shall ONLY be made once the FileUploadDate reaches the last day of the month. In other words when the FileUploadDate is EQUAL to the last day of the month. The main issue here is when the first condition of the WHERE clause is TRUE, the second condition is "Filtered" with values from the first condition, which makes it irrelevant. I tried using other methods such as CASE-WHEN-THEN, but I'm having problems integrating DELETE inside a CASE clause (if that is even possible). Is there a way to perform two separate conditions without one affecting the other? Thanks.
DELETE
FROM transacions
WHERE EXISTS
(
SELECT
*
FROM transacions
WHERE fileuploaddate = CONVERT( date, dateadd(d, -2, dateadd(m, datediff(m, 0, getdate()) + 1, 0)),103)
AND fileuploaddate BETWEEN CONVERT(date, dateadd(month, datediff(month, 0, getdate()), 0))
AND CONVERT(date, dateadd(d, -1, dateadd(m, datediff(m, 0, getdate()) + 1, 0)),103)
)
you could use OR instead AND
WHERE FileUploadDate = CONVERT(DATE, DATEADD(d, -2, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)),103)
OR FileUploadDate BETWEEN CONVERT(DATE, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) AND CONVERT(DATE, DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)),103)
Try this:
DELETE
FROM Transactions
WHERE EOMONTH (FileUploadDate) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) -- delete on the last day of the month
AND FileUploadDate >= DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) -- delete files from the begining of the month
AND FileUploadDate <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) -- delete files to the end of the current montn

Using DATEADD and DATEDIFF truncates the time

I am using the following to get to get the 1st day of next month with time:
select DATEADD(m, DATEDIFF(m, -1, getdate()), 0)
But the output is:
2018-12-01 00:00:00.000
And the expected result is:
2018-12-01 11:53:30.677
I have tried various approaches but not able to get required output. I am using SQL Server 2008.
You can add two datetime values, one for the date and the other for the time:
select DATEADD(month, DATEDIFF(month, -1, getdate()), 0) + cast(cast(getdate() as time) as datetime)
I am guessing that you want the time value from the current time.
Subtract DAY(#date) - 1 days from #date to get first day of that month including time. Then add one month:
SELECT DATEADD(MONTH, 1, DATEADD(DAY, -DAY(GETDATE()) + 1, GETDATE()))
-- 2018-12-01 04:52:33.403
try like below
select DATEADD(m, DATEDIFF(m, -1, getdate()), 0)+ convert(DATETIME,'11:53:30.677')
in case of current time it would be like below
select DATEADD(m, DATEDIFF(m, -1, getdate()), 0)+ convert(datetime, convert(time,getdate()))

Pass current month date in parameter and get get prior month data

I tried this but no desired result in where clause
Convert(varchar(7), dateadd(mm, -1, getdate()), 120)
=Convert(varchar(7), #Paramtername, 120)
In SQL Server:
To get the start of the prior month, you can use
dateadd(month, datediff(month, 0, getdate() )-1, 0)
To get data for the prior month of a parameter you could use something like this:
where mydate >= dateadd(month, datediff(month, 0, #date_parameter )-1, 0)
and mydate < dateadd(month, datediff(month, 0, #date_parameter ) , 0)

Week of the Month in SQL

DECLARE #date DATETIME= GETDATE()
SELECT DATEDIFF(WEEK,
DATEADD(WEEK,
DATEDIFF(WEEK, 0,
DATEADD(MONTH, DATEDIFF(MONTH, 0, #date), 0)),
0), #date - 1) + 1
What is purpose of 0 as a parameter in the datediff() function?
The specific answer to your question is that the 0 is just a way to get the beginning of the month:
dateadd(month, datediff(month, 0, #date), 0)
This is one method to do this in SQL Server, because it does not offer a "date truncate" function. I prefer:
dateadd(day, 1 - day(#date), #date)
(Although admittedly this is a wee bit more complicated if #date has a time component.)
However, a much simpler way to do this is:
select (day(#date) - 1) / 7) as week_of_month
Below gets the number of months from a reference point
datediff(month, 0, #date)
Then it is used to add to the reference date back to reach the first day of the month
dateadd(month,
datediff(month, 0, #date),
0)
So it is used to find the first day of the current month
declare #date datetime = getdate()
select
getdate(),
datediff(month, 0, #date),
dateadd(month,
datediff(month, 0, #date),
0)

SQL - Get data based on months form a dateTime column

With SQL Server, I have a column with a launch date (dateTime). I want to report on everything that is being launched between all of last month (from viewing date) thru all of this month and next month.
So basically a full 3 month period.
What is the best way to write that?
Are you looking for something like this?
DECLARE
#StartDate DATETIME,
#EndDate DATETIME
SELECT
#StartDate = DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 1, 0),
#EndDate = DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 2, 0)
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0), -- beginning of this month
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 1, 0), -- beginning of last month
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 1, 0) -- beginning of next month
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 2, 0) -- beginning of two months from now
SELECT
*
FROM
[Table]
WHERE
[LaunchDate] >= #StartDate
AND [LaunchDate] < #EndDate
This will give you all the results starting from the beginning of the previous month and before the beginning of two months from now (a full 3 month range)
Maybe something like SELECT /*what_you_want*/ from launches WHERE lauchDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()) - 1, '19000101') AND DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()) + 2, '19000101')
SELECT Foo
FROM Bar
WHERE LaunchDate >= DATEADD(mm, -1, GETDATE())
AND LaunchDate <= DATEADD(mm, 1, GETDATE())