MS Sql Date format? - sql-server

I'm pretty new to MSSQL and was wondering if someone could help me convert the following where statement from looking at previous week to previous month.
ks.report_date BETWEEN (convert(varchar(113), (DATEADD(day, (-1 * DATEPART(dw, (dateadd(week, -1, getdate())))) + 1, (dateadd(week, -1, getdate())))), 101))
AND (convert(varchar(113), (DATEADD(day, (-1 * DATEPART(dw, (dateadd(week, -1, getdate())))) + 7, (dateadd(week, -1, getdate())))), 101))

SQL Server has a function called EOMonth() that will take a date and give you the end of the month containing that date. You can then use DATEADD to subtract months from that value to get the last day of previous months. Lastly, use DATEADD again to add 1 day to get the first day of the month. Here is an example.
BETWEEN DATEADD(DAY, 1, DATEADD(MONTH, -2, EOMONTH(GETDATE())))
AND DATEADD(MONTH, -1, EOMONTH(GETDATE()))

If you're using an earlier version than SQL Server 2012 then this would work:
BETWEEN DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
AND DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))

Related

How can i get dates Saturday to Friday using SQL getdate()

How can I get use gatedate() to always pick up the last Saturday to current Friday.
When I try to use the code I getting a static Saturday date
I have tried this way
Select
cast dateadd(y,datediff(y,0,getdate()),-1 ) as date) as Satuday,
cast(dateadd(d,-3,dateadd(week,datediff( week,0,getdate()),0)) as Friday
example
Saturday date = 31/12/2023
Friday date = 6/01/2022
Try the following code, you can adjust the code...
SELECT DATEADD(DD, 12, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -6, CURRENT_TIMESTAMP)) -1, 0)) -- Saturday
SELECT DATEADD(DD, 18, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -6, CURRENT_TIMESTAMP)) -1, 0)) -- Friday

Applying filter to sum quarterly data

I would like some help with the filter for summing Quarterly data.
For Yearly data I have added the formula.
For Quarterly data, I have hard coded for now but need to get the system to calculate.
So for example next quarter which will be Quarter 3, I need to calculate figures for current year/current quarter (summing figures for 3 months in that quarter)
SNIPPET of filter code
WHERE [Status] = 'Complete'
AND (YEAR([datefield]) = YEAR(GETDATE()) - 1
OR YEAR([datefield]) = YEAR(GETDATE()) - 2
OR YEAR([datefield]) = YEAR(GETDATE())
AND MONTH([datefield]) IN(4, 5, 6)) --Apr/May/June.
Sorry for providing an incomplete answer. I have thought of this after I've answered.
SELECT *
FROM table
WHERE [Status] = 'Complete'
AND [datefield] BETWEEN CONVERT(date, DATEADD(M, -2, DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE())))
AND EOMONTH(DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE()))
Explanation:
For the filter to be accurate, you need to get the first day and last day of the month.
First Day:
Get current date:
SELECT GETDATE()
2018-09-06 17:03:35.467
Get current date minus 1
SELECT DATEADD(D, -1, GETDATE())
2018-09-05 17:03:35.467
Subtract "current date minus 1" (the above result) to the current date.
SELECT DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE())
2018-09-01 17:03:35.467
Get date part only:
SELECT CONVERT(date, DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE()))
2018-09-01
Last Day:
Same as getting the first day, but changing CONVERT to EOMONTH since the latter already returns the result as date part only
SELECT EOMONTH(DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE()))
2018-09-30
So you now have
SELECT CONVERT(date, DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE()))
,EOMONTH(DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE()))
2018-09-01
2018-09-30
Get the previous 2 months from the current month.
SELECT CONVERT(date, DATEADD(M, -2, DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE())))
Notice that I just added DATEADD(M, -2, ) to the First Day value.
Combine the first day of 2 months ago and the last day of the current month you get
SELECT CONVERT(date, DATEADD(M, -2, DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE())))
,EOMONTH(DATEADD(D, -DAY(DATEADD(D, -1, GETDATE())), GETDATE()))
2018-07-01
2018-09-30

Return records from this week

SELECT *
FROM TABLE TB
WHERE TB.DATE >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND TB.DATE < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
My dates look like this: 2015-01-19 00:00:00
Currently my query returns no records. I want to return records from this week. Anything created from Monday to Sunday.
I am using SQL Server 10.50.1617
Try this, it will find all data from this week from monday to sunday:
SELECT *
FROM TABLE TB
WHERE
TB.DATE >= dateadd(d, 0, datediff(d, 0, current_timestamp)/7*7)
AND TB.DATE < dateadd(d, 7, datediff(d, 0, current_timestamp)/7*7)
This will work for current week Monday - Sunday:
SELECT *
FROM TABLE TB
WHERE TB.DATE >= (DATEADD(day, DATEDIFF(day, 1, getdate()) / 7 * 7, - 0))
AND TB.DATE <= DATEADD(dd,DATEDIFF(dd,0,getdate())+ 1 , + 2)
This will get you prior week Monday - Sunday:
SELECT *
FROM TABLE TB
WHERE TB.DATE >= DATEADD(wk, DATEDIFF(wk, 6, GETDATE())-1, 7)
AND TB.DATE <= (DATEADD(day, DATEDIFF(day, 1, getdate()) / 7 * 7, - 1))

SQL Date clause for Reporting Services

I am developing a report using SQL and SSRS that gives a day by day breakdown of stats from the 1st of the month to the previous day.
However, on the 1st of the month the report comes up blank when I need it to show the previous months information (e.g. on the 1st of February I want a report that gives me a day by day breakdown of January from the 1st to 31st.
I tried the following case statement in the SQL where clause to see if it would fix the issue but it doesn't seem to work:
(dIntervalStart BETWEEN dbo.DateAndTime(CASE WHEN DAY(GEtDate()) = 1 THEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE() - 1), 0) ELSE DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) END, '00:00:00')
Is there something else that might work?
declare #StartOfMOnth bit
declare #startDate date
declare #endDate date
select #StartOfMOnth =
case when datepart(dd, getdate()) <> 1 then 0
else 1
end
IF (#StartOfMOnth = 0) --If not start of month
BEGIN
SELECT #startDate = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) --First day of current month
SELECT #endDate = DATEADD(DD, -1, GETDATE())--Previous day
END
ELSE --If start of month
BEGIN
SELECT #startDate = DATEADD(MONTH, -1, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) --First day of last month
SELECT #endDate = DATEADD(DD, -1, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))--Last day of last month
END
--Once you have start and end dates, do what needs to be done in the dataset

Deterministic scalar function to get week of year for a date

Here is a great way of how to get day of week for a date, Deterministic scalar function to get day of week for a date.
Now, could anyone help me to create a deterministic scalar function to get week of year for a date please? Thanks.
This works deterministically, I can use it as a computed column.
datediff(week, dateadd(year, datediff(year, 0, #DateValue), 0), #DateValue) + 1
Test code:
;
with
Dates(DateValue) as
(
select cast('2000-01-01' as date)
union all
select dateadd(day, 1, DateValue) from Dates where DateValue < '2050-01-01'
)
select
year(DateValue) * 10000 + month(DateValue) * 100 + day(DateValue) as DateKey, DateValue,
datediff(day, dateadd(week, datediff(week, 0, DateValue), 0), DateValue) + 2 as DayOfWeek,
datediff(week, dateadd(month, datediff(month, 0, DateValue), 0), DateValue) + 1 as WeekOfMonth,
datediff(week, dateadd(year, datediff(year, 0, DateValue), 0), DateValue) + 1 as WeekOfYear
from Dates option (maxrecursion 0)

Resources