Get last 3 thursday dates based on the date we input - sql

I was trying to get the last three Thursday dates based on the date which I gave. I got it right if i insert a Tuesdays date but does not work for a Friday date. What i did was:
select dt from (select *,ROW_NUMBER() over (order by(d.dt)) as rn from
(SELECT Top 4 CONVERT(varchar, DATEADD(Month, -1, '5/29/2012')+number,101) as dt
FROM master..spt_values WHERE TYPE ='p' AND
DATEDIFF(d,DATEADD(Month, -1, '5/29/2012'),'5/29/2012') >= number
AND DATENAME(w,DATEADD(Month, -1, '5/29/2012')+number) = 'Thursday') as d )
as nw where nw.rn>1
and the output I get is 5/10/2012, 5/17/2012, 5/24/2012.
But when I change the date to 5/18/2012 the output i get is
04/26/2012, 05/03/2012, 05/10/2012
But it should be 05/3, 05/10, 05/17.....What is wrong or is there any other method to do this?

Just replace DATEADD(Month, -1, with DATEADD(Week, -4, and it starts working for Fridays and Saturdays.
Explanation: If you subtract a Month it usually substracts 30 or 31 days, which adds an extra Thursday if the start date is a Friday, Saturday or (in the 31 days case a Sunday). After that the Top 4 cuts the last Thursday in the list.
Edit, to just select three values:
select * from (select *,ROW_NUMBER() over (order by(d.dt)) as rn from
(
SELECT CONVERT(varchar, DATEADD(Week, -3, '5/25/2012')+number,101) as dt
FROM master..spt_values WHERE TYPE ='p'
AND DATEDIFF(d,DATEADD(Week, -3, '5/25/2012'), DATEADD(Day,-1,'5/25/2012')) >= number
AND DATENAME(w,DATEADD(Week, -3, '5/25/2012') + number) = 'Thursday') as d
)
as nw

Related

Having Thursday and Wednesday as Start and end date of the week for SQL Server

I am having a bit of struggle converting the start and end date of the week into Thursday and Wednesday into SQL Server.
I have figured out to get a code to extract the Thursday and Wednesday of the week
SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 3) ThursdayOfCurrentWeek
however the issue is that the current Thursday's week start should be itself but the code returns the Thursday from last week.
For example, preferably it should be:
29/09/2021 (23/09/2021-29/09/2021);
30/09/2021 (30/09/2021-06/09/2021)
but SQL will return:
29/09/2021 (23/09/2021-29/09/2021);
30/09/2021 (23/09/2021-29/09/2021)
Is there a way that I could do this?
..fiddle..
select
_date, datename(weekday, _date) _dateweekday,
prvthursday, datename(weekday, prvthursday),
cast(prvthursday as date) as week_start, cast(dateadd(day, 6, prvthursday) as date) as week_end
from
(
select *, dateadd(day, -(7+datepart(weekday, dateadd(day, ##datefirst, _date))-5/*<-- prev thursday:5, prev sat:7, prev monday:2 etc*/)%7, _date) as prvthursday
from
(
--dates
select dateadd(day, row_number() over(order by ##spid), '20210101') as _date
from sys.all_objects
) as d
) as src;

SQL query to find dates of last year equivalent to today's date

I am writing a SQL query to find business working dates of last year equivalent to today's date.
In this query it should fetch :-
For e.g. if today is 5th January, 2021 and it is the second day of second week of the year. So I need to find the exact equivalent date of the second day of second week of the previous year. So it would be 7th January, 2020.
And with this, I need the business working dates of that week of 7th January 2020 (i.e. excluding Saturday & Sunday)
Which will come up as 2020-Jan-06 to 2020-Jan-10 according to the example.
So I will need the report between 6th Jan - 10th Jan, 2020.
I am trying to use this code to find date of last year equivalent to today's date (5th Jan, 2021 viz. second day of second week)
select Convert(date, (DATEADD(year, -1, getdate()+2))) ;
2021-01-05 is the 2nd day of the first week of 2021 according to ISO standards.
If you want the 2nd day of the first week of 2021, then it is either today's date minus 52 weeks or 53 weeks. Based on the Wikipedia page for ISO dates:
[53 week years are those] years in which 1 January or 31 December are Thursdays
So, we want that for the previous year. Hence, I think the following should work:
select dateadd(week,
(case when 'Thursday' in (datename(weekday, datefromparts(year(getdate()) - 1, 1, 1)),
datename(weekday, datefromparts(year(getdate()) - 1, 12, 31))
)
then -53 else -52
end),
convert(date, getdate())
)
Note that this returns 2019-12-31, which is the correct value based on ISO standards.
I have use multiple CTE to show you the step by step calculation. It should be pretty easy to follow.
Basically it find the week_no and day_no_of_week for 2021-01-05 and then use that to find the same date for 2020
declare #input_date date = '2021-01-05',
#year_offset int = -1; -- previous year
with
cte1 as
(
select input_date = #input_date,
week_no = DATEPART(WEEK, #input_date),
first_day_of_week = DATEADD(WEEK, DATEDIFF(WEEK, 0, #input_date), 0)
),
cte2 as
(
select *,
day_no_of_week = DATEDIFF(DAY, first_day_of_week, #input_date) + 1
from cte1
),
cte3 as
(
select *,
first_day_of_the_prev_year = DATEADD(YEAR, DATEDIFF(YEAR, 0, #input_date) + #year_offset, 0)
from cte2
),
cte4 as
(
select *,
first_day_of_week_prev_year = DATEADD(WEEK, DATEDIFF(WEEK, 0, DATEADD(WEEK, week_no - 1, first_day_of_the_prev_year)), 0)
from cte3
)
select *,
DATEADD(DAY, day_no_of_week - 1, first_day_of_week_prev_year) as the_required_date
from cte4

How can I select the past seven days and its corresponding week in the past year

How can I select
The past week
Its corresponding days in the year before
This is needed for a dashboard, I would like to show a chart with results from the past seven days. It displays green if our call-center handles 98% of their phone calls within a certain time-span, red if we go over 98%. As a reference I would like to create a chart below with the corresponding seven days in the year before. This is challenging, because weekdays really influence the workload. That means I can't compare a Tuesday with a Sunday or Monday.
For instance, today is Saturday 21st Dec 2019, I would like to report the following timespans:
2019-12-13 00:00:00 -> 2019-12-20 23:59:59
and
2018-12-14 00:00:00 -> 2018-12-21 23:59:59
I made the following code (used within a select statement):
case when cs.ReachedAt between (getdate() - 7) and getdate() then 1 else 0 end as Is_PastWeek
case when cs.ReachedAt between (convert(datetime, convert(varchar(50), convert(date, dateadd(d, -1, dateadd(wk, -52, getdate())))) + ' 23:59:59')) and (convert(datetime, convert(varchar(50), convert(date, dateadd(d, -8, dateadd(wk, -52, getdate())))) + ' 00:00:00')) then 1 else 0 end as Is_SameWeekLastYear
It works, but isn't perfect. I just select the corresponding weekday in the same week as 52 weeks ago. Which means I sometimes end up selecting a matching weekday, but not the nearest. How can I do this better?
EDIT
To clarify what I mean by "picking the nearest corresponding weekday in the year before", i made the following example:
with cte1 as (
select row_number() over (order by (select 1)) - 1 as incrementor
from master.sys.columns sc1
cross join master.sys.columns sc2
), cte2 as (
select dateadd(day, cte1.incrementor, '2000-01-01') as generated_date
from cte1
where dateadd(day, cte1.incrementor, '2000-01-01') < getdate()
), cte3 as (
select convert(date, generated_date) as generated_date
, convert(date, getdate()) as now_date
from cte2
), cte4 as (
select *
, convert(date, dateadd(YEAR, -1, now_date)) as year_back
from cte3
)
select now_date
, generated_date
from cte4
where 1=1
and datepart(week, year_back) = datepart(week, generated_date)
and datepart(DW, year_back) = datepart(DW, generated_date)
This will result in:
For the grey values, I would rather take the weekday of one week later. That way I pick "the nearest corresponding weekday in the year before".
Please note that the above is an example to show what I mean, my ultimate goal is to start with this date, select the whole week before... And all (if possible) neatly within a where clause.
The expression datepart(week, getdate()) will deliver you the calendar week. With this, you can go further.
This is too long for a comment.
What difference does it make? If you are looking for the past week, just look at the same 7 days from the previous year. In one case the week might start on a Tuesday and in the other on a Wednesday. But in both cases, each weekday occurs once.
The logic would be:
where cs.ReachedAt >= datefromparts(year(getdate() - 7) - 1, month(getdate() - 7), day(getdate() - 7) and
cs.ReachedAt < datefromparts(year(getdate()), month(getdate()), day(getdate()))
The logic for the current year:
where cs.ReachedAt >= convert(date, getdate() - 7) and
cs.ReachedAt < convert(date, getdate())

First date of current Fiscal Year

I am trying to get the First Date of the current Fiscal year. In my case the Fiscal year starts in Oct.
Example: I need the 10/01/2015
I would generally use the below query to the get the first day of the current year but how do I change it to get the first date of fiscal year?
select
convert(varchar(12), (
DateAdd(month, (
Month(getdate()) - 1) * -1,
DateAdd(Day, (
Day(getdate()) - 1) * -1,
getdate()))),
103) as StartYear
You can apply following logic: Subtract 9 months from the current date, find Jan 1st of this year and add 9 months again:
DATEADD(MONTH, 9, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(MONTH, -9, GETDATE())), 0))
declare #fiscal date = '2015-10-01'
; with dates
as
(
select date = '2015-09-30' union all
select date = '2015-10-01' union all
select date = '2016-09-30' union all
select date = '2016-10-01'
)
select *,
fiscal = case when date < dateadd(year, year(date) - year(#fiscal), #fiscal)
then dateadd(year, year(date) - year(#fiscal) - 1, #fiscal)
else dateadd(year, year(date) - year(#fiscal), #fiscal)
end
from dates
/* result
date fiscal
---------- ----------
2015-09-30 2014-10-01
2015-10-01 2015-10-01
2016-09-30 2015-10-01
2016-10-01 2016-10-01
*/
At first Subtract 9 months from current date to get the previous year(your Fasical year).
Then take the year difference with 0 to your date.
Now add the year difference with 0 to get the starting of a month.
Finally you need to add the 9 months to get the starting month of a Fasical Year.
As year example the require format "10/01/2015". So get that you can use convert function with 101.
select convert(varchar(12), DATEADD(MONTH, 9, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(MONTH, -9, GETDATE())), 0)), 101) as StartYear

SQL Server 2008 Count(Distinct?

ApptStart
2005-02-18 10:00:00.000
2005-02-18 13:00:00.000
2005-02-18 11:00:00.000
2005-02-21 09:00:00.000
2005-02-18 15:30:00.000
2005-02-18 14:30:00.000
.
.
.
I have a column in our database that looks like the above. I want to count appointments for the month for a given doc. On Fridays most of them do a half day. So I do not want to count Fridays with appointments only in the morning. If the appointment is in the afternoon, after 12:00:00.000 I want to include that day in the distinct count.
So far I have:
SELECT
ScheduleDays = count(distinct CONVERT(datetime, convert(char(12), a.ApptStart, 1)))
FROM Appointments a
WHERE
ApptKind = 1 AND
--filter on current month
a.ApptStart >= ISNULL(DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0),'1/1/1900') AND
a.ApptStart < ISNULL(DATEADD(month, DATEDIFF(month, 0, GETDATE())+1, 0),'1/1/3000') AND
--Filter on doctor
a.ResourceID in (201)
This worked when I counted every day with appointments on it, but like I said I need to exclude those half days.So I was thinking about only looking at the last right chars of the ApptStart and comparing it x > noon in a case inside the distinct count...
I tried the following, but it did not work:
ScheduleDays = count(distinct case when (Right(a.ApptStart, 12)) > '12:00:00:000' then 1 else 0 END)
Thanks in advance!
EDIT
I tried:
SELECT
ScheduleDays=COUNT(DISTINCT(CAST(datediff(d,0,a.ApptStart) as datetime)))
FROM Appointments a
WHERE
ApptKind = 1 AND
--filter on current month
a.ApptStart >= ISNULL(DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0),'1/1/1900') AND
a.ApptStart < ISNULL(DATEADD(month, DATEDIFF(month, 0, GETDATE())+1, 0),'1/1/3000')AND
--filter all days that aren't Friday, and then give you all Fridays with an hour > 12.
DATENAME(weekday, a.ApptStart) <> 'Friday' OR DATEPART(hour, a.ApptStart) > 12 AND
--Filter on doctor
a.ResourceID in (201)
for 1808 as the count
You probably want to treat dates as dates, rather than strings. You can determine whether a particular timestamp is on a Friday -- or the hour of a timestamp -- using DATEPART, without having to CONVERT it into a CHAR:
datename(weekday, timestamp_value) -- returns Friday
datepart(weekday, timestamp_value) -- returns either 5 or 6, depending on the value of SET DATEFIRST. (Get day of week in SQL 2005/2008)
datepart(hour, timestamp_value) -- returns hour part
Using these, you can test whether a timestamp is on Friday at or after noon by checking if datepart(weekday, timestamp_value) = 6 and datepart(hour, timestamp_value) >= 12.
bendataclear pointed out that you're using distinct on a case statement which can only ever return 0 or 1, so your total will only ever be 0, 1, or 2. If you're trying to determine which days the doctors worked more than half a day, you'll need to select distinct dates --
SELECT COUNT(DISTINCT(CAST(datediff(d,0,timestamp_value) as datetime)))
FROM table_name
WHERE DATENAME(weekday, timestamp_value) <> 'Friday' OR DATEPART(hour, timestamp_value) > 12
AND the rest of your filters here
The WHERE clause there will give you all days that aren't Friday, and then give you all Fridays that have an hour > 12.