Pulling last week M-F range from any current week day - sql

SELECT d.i_EmpID as 'EmpID',m.c_EmployeeFirstName as 'First Name',
m.c_employeeLastName as 'Last Name', d.c_date as 'Date', d.c_A as 'A', d.c_M
as 'M', d.c_P as 'P', d.c_lp as 'Lp'
,'School' as Company
FROM [table1].[dbo].[tbl_daily] d
inner join [table2].[dbo].[tbl_emp] m
on d.[i_EmpID] = m.[i_EmpID]
where left(c_date, 2)=datepart(month, getdate()-1) and
right(c_date,4)=datepart(year,getdate()-1) and substring(c_date,
4,2)=datepart(day,getdate()-3)
Hello,
I am trying to pull last week date range past off of current date. Right now I have 5 separate queries unioned for the end of the code (day,getdate()-3) to be the other 5 days in the week (I am looking for Monday to Friday). This is essentially my query without the other 4 unions to be able to pull Monday to Friday and I already this is not the best way at all to do it and in fact it doesn't even work today.
I am able to get this done in MySQL because it has a built in week function and I can just pull last week, however I can't figure out how to pull last week in SQL because the language is different. My issue is that my code worked yesterday, however due to the fact it is subtracting the days, on Tuesday to Friday, it won't work/it doesn't work. I am by no means an expert with using dates in SQL and I am at a loss.
I know there has to be a better way, I've spent about 4 hours reading various guides and examples but I cannot seem to find an example of specifically what I am looking for.
Long and short of it is that I am trying pull last week (example: 9/24 to 9/28) any day of the current week that I run it. So even if I run my report on Friday(10/5), I am able to see 9/24 to 9/28 and the similarly next week to be able to pull 10/1 to 10/5 of this week. Those are the tables and columns I am pulling. Also, if it matters, this is not a school project but a work related project I was tasked with.
Any guidance would be greatly appreciated. Thank you in advanced.
edit: Sample Data:
EID First Last Date A M P LP Company
5303 Kent Grit 09/25/2018 9 44 54 3 Company 1
6890 Lola Grif 09/28/2018 5 4 3 Company 1
6272 Paul Hammer 09/26/2018 1 2 4 4 Company 1
6273 Joe Hammer 09/24/2018 11 8 7 Company 1

Is this what you're looking for?
...
WHERE
CAST(c_date AS DATE) >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE())-1, 0)
AND
CAST(c_date AS DATE) < DATEADD(DAY, +5, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE())-1, 0))
It will pull last Monday at midnight and last Saturday at midnight (so you get the last second of Friday in your result set) from Sunday through Saturday.
Edit: Added the CAST per comments.

This can be accomplished with dateadd and datepart functions with some arithmetic.
with dates(dt) as (select '2018-10-02' union all
select '2018-10-01' union all
select '2018-10-03' union all
select '2018-10-04' union all
select '2018-10-05' union all
select '2018-10-06' union all
select '2018-10-11'
)
--Actual query to be executed by replacing the table name
select dt,
dateadd(day,-datepart(dw,cast(dt as date))-5,cast(dt as date)) as strt,
dateadd(day,-datepart(dw,cast(dt as date))-1,cast(dt as date)) as ed
from dates --replace this with the table being used

Related

Recurring Date Calculator SQL

I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);

SQL Server: Count number of records on weekly basis (Week = Thursday to Wednesday)

I need some help in writing an SQL in SQL Server where I need to count number of rows group by weeks. There is a tricky description of week which is following
- For any date before 08/13/2015 the week is of 7 days (i.e. from Thu through Wed)
- For date 08/13/2015 the week is consider a 9 day week (i.e. from Thursday through Friday so its between 08/13/2015 through 08/21/2015)
- For date 08/22/2015 the week is back to 7 days (i.e. Sat through Friday)
Now having said all the above the result I want to see in my report is the following way . NOTE: WE column in the below attached image is the last day of the week for the range.
Sample Result Image
Just write a case statement for the 3 different options. You can find the start day with something like this:
DATEADD(week, DATEDIFF(day, 3,getdate()) / 7, 3) -- Thursdays
DATEADD(week, DATEDIFF(day, 5,getdate()) / 7, 5) -- Saturdays
The numbers 3 and 5 come from the fact that day 0 (=1.1.1900) is Monday.
If you use this a lot, it might be a good idea to write a inline table valued function to return the dates you need.

Get month name with different month start and end dates

Getting the month for the current date is, obviously, straight forward, but I'm needing to get the month name with a different end date.
I need to get the month name with the start date of the month being the first Thursday after the first Wednesday of the month and the end date of the month being the first Wednesday of the following month. It's for an accounting thing, so I'm not going to argue with the spec!
e.g. for 2014, January would run from 9th Jan - 5th Feb, February would run from 6th February - 5th March, March would run from 6th March - 2nd April.
I would suggest that you create a table with your 'accounting months' in it, having a start date, end date and month name columns.
You could then query this to find the row where your date is between the start and end dates and return the month name. Putting this into a scalar function would then allow it to be reusable and relatively easily updated for next years months as well.
I think, as per Paddy's answer, a lookup table is the simplest thing to do. Here's one way to generate the rows for it:
; With Numbers(n) as (
select 4 union all select 5
), Months as (
select CONVERT(date,'20010104') as StartDt,CONVERT(date,'20010207') as EndDt,
DATENAME(month,'20010103') as Month
union all
select DATEADD(week,n1.n,StartDt),DATEADD(week,n2.n,EndDt),
DATENAME(month,DATEADD(week,n1.n,StartDt))
from Months,Numbers n1,Numbers n2 --Old-skool join, just for once
where DATEPART(day,DATEADD(week,n1.n,StartDt)) between 2 and 8 and
DATEPART(day,DATEADD(week,n2.n,EndDt)) between 1 and 7 and
StartDt < '21000101'
)
select * from Months option (maxrecursion 0)
(CW since this is effectively just an extension to Paddy's answer but I don't want to edit their answer, nor is it suitable for a comment

How to find if a date is from a Odd or Even week?

In my table I have for each day:
Id_______Startdate _______ EndDate __________MondayMorning _____MondayEvening ___TuMorning ....
121 _____2012-01-01________2012-12-31 ________2 ___________________2______________2
122 _____2012-02-01________2012-08-05 ________1 ___________________2______________3
I already generated a list of dates using the Startdate and EndDate.
I want to know if this dates belongs to a odd week or a even week so that I can filter days from my output that have number 3 Or 1 (see second record).
how can I filter the days that belongs to odd weeks and even weeks that have number 1 and 3?
You can use datepart with the wk argument to determine the week number:
SELECT datepart(wk, YourDate)
From there, you can use modulus to determine if the week number is even or odd:
SELECT datepart(wk, YourDate) % 2
This will return 0 for even numbered weeks, and 1 for odd numbered weeks.

DateDiff SQL Server Problem

Could some one please explain this to me as I am a touch confused as to why this is happening? Basically what I would like to know is why there is a difference between Sundays date and every other day of this week in weeks from the year 0. If that makes sense!
I tried setting the date first but this had no effect. Surely Monday – Sunday of this week should all have the same difference in weeks from the year zero?
Set Datefirst 1
Select DateName(dw,0) --Monday
Select DateDiff(week, 0, '20091109')--Monday: Difference 5732
Select DateDiff(week, 0, '20091114')--Saturday: Difference 5732
Select DateDiff(week, 0, '20091115')--Sunday: Difference 5733
What makes this even more bizarre is if you take the same two dates and date diff them you get one week for the one and 6 days for the other. Am I missing something here?
Select DateDiff(dd,'20091109','20091115')--6 Days difference
Select DateDiff(ww,'20091109','20091115')--1 Week difference
I am using SQL Server 2005
Sunday is considered the first day of the week in a number of countries.
All the datediff functions do is count the number of date boundarys between the two datetime arguments passed to it. So if a week is defined to start Sunday, then the week boundary is midnight Sunday Morning.
So, From 11:59 Saturday night to 00:01 am Sunday Morning, will be the same datediff(week, x, y) as from 00:01 Sunday to 11:59 PM Saturday Night - 13 days later.
x ------------------------------x ==> 1 week diff
| Su M T W T F S | Su M T W T F S |
| | |
x-x ==> Also 1 week diff
I think the problem you are facing is the boundary dates. Are they inclusive?
Select DateDiff(dd,'20091109','20091115')--6 Days difference
Select DateDiff(dd,'20091109','20091116')--7 Days difference
DateDiff - Returns the number of date and time boundaries crossed between two specified dates. (Books Online Definition)
If you Set Datefirst = 1, Monday is the first day of the week, and the week calculations would use MOD 7 for number of days in the week and /7 for week count, in which sunday would be used as 1, and the others FLOORed to 0.