SQL setting start date to Friday, and read Data between 2 dates - sql

I'm trying to set the start date to friday on a sql query. Which I have done as you can see below. What I need to do now is show all the GameID's between Friday and Saturday, and it refreshes every week (so that every week it shows other games that have been played in that week).
I'm a complete beginner at SQL so any help is greatly appreciated!
I have tried the sql query below.
DECLARE #StartFriday datetime
DECLARE #EndSaturday datetime
SET DATEFIRST 6 -- Set the start of the week to Friday
SELECT *
FROM
(
SELECT GameDate,
DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), -5)) AS 'StartFriday',
DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), 1)) AS 'EndSaturday'
FROM VW_Resultaat_Score
WHERE GameDate BETWEEN 'StartFriday' AND 'EndSaturday' --Show all GameDates between #StartFriday and #EndSaturday
)
I would love any help I can get!
Cheers

Perhaps this will work better:
DECLARE #StartFriday datetime
DECLARE #EndSaturday datetime
SET DATEFIRST 6
Set #StartFriday = DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), -5))
Set #EndSaturday = DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), 1))
SELECT *
FROM
(
SELECT GameDate
FROM VW_Resultaat_Score
WHERE GameDate BETWEEN #StartFriday AND #EndSaturday
)

If you will only ever need the weekend previous, this code will work:
SELECT *
FROM VW_Resultaat_Score
WHERE GameDate BETWEEN
(SELECT DATEADD(d, 1 - datepart(weekday, dateadd(d, 2, getdate())), getdate()))
AND
(SELECT DATEADD(d, 1 - datepart(weekday, dateadd(d, 1, getdate())), getdate()))
Code looks a little crazy, but it works. If you may need to query other specific weekends, this may be an option:
DECLARE #getdate date
SET #getdate='2017-05-16'
SELECT *
FROM VW_Resultaat_Score
WHERE GameDate BETWEEN
(SELECT DATEADD(d, 1 - datepart(weekday, dateadd(d, 2, #getdate)), #getdate))
AND
(SELECT DATEADD(d, 1 - datepart(weekday, dateadd(d, 1, #getdate)), #getdate))
Taking it a step further, you may need to report on each of these weekends. The following code gives you all the Friday Saturdays of each week for 2017.
WITH Fri as
(SELECT DATEPART(YEAR,DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)) yyyy, DATEPART(wk,DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)) weeknumber, Fridays = CAST(DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num) as Date)
FROM (SELECT TOP 366 num = ROW_NUMBER() OVER(ORDER BY a.NAME)-1 FROM dbo.syscolumns a, dbo.syscolumns b) n
WHERE DATENAME(weekday, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)) = 'Friday')
,
Sat as
(SELECT weeknumber=DATEPART(wk,DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)), Saturdays = CAST(DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num) as Date)
FROM (SELECT TOP 366 num = ROW_NUMBER() OVER(ORDER BY a.NAME)-1 FROM dbo.syscolumns a, dbo.syscolumns b) n
WHERE DATENAME(weekday, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)) = 'Saturday')
Select Fri.weeknumber, fri.yyyy, Fridays, Saturdays
FROM Fri
JOIN Sat on Fri.weeknumber=Sat.weeknumber

Related

Get first day and month for last x calendar weeks

I want to get last 8 week starting from today ( GETDATE() )
So the format must be dd/mm for all 8 weeks.
I tried something like this
select "start_of_week" = cast(datepart(dd,dateadd(week, datediff(week, 0, getdate()), 0)) as CHAR(2))+'/'+cast(datepart(mm,dateadd(week, datediff(week, 0, getdate()), 0)) as CHAR(2));
and this is good only for the current week, but how to put this in query and return for curr-1,curr-2,...curr-7 weeks. The final result must be table with some amounts for one player and each week in format dd/mm
Maybe as easy as this?
WITH EightNumbers(Nmbr) AS
(
SELECT 0
UNION SELECT -1
UNION SELECT -2
UNION SELECT -3
UNION SELECT -4
UNION SELECT -5
UNION SELECT -6
UNION SELECT -7
UNION SELECT -8
)
SELECT CONVERT(VARCHAR(5),GETDATE()+(Nmbr*7),103)
FROM EightNumbers
ORDER BY Nmbr DESC
If you need (as the title suggests) the "first day" of the week, you might change the select to:
SELECT CONVERT(VARCHAR(5),GETDATE()+(Nmbr*7)-DATEPART(dw,GETDATE())+##DATEFIRST,103)
FROM EightNumbers
ORDER BY Nmbr DESC
Be aware that the "first day of week" depends on your system's culture. Have a look on ##DATEFIRST!
The result:
28/12
21/12
14/12
07/12
30/11
23/11
16/11
09/11
02/11
Here you go:
DECLARE #DateTable TABLE ( ADate DATETIME )
DECLARE #CurrentDate DATETIME
SET #CurrentDate = GETDATE()
WHILE (SELECT COUNT(*) FROM #DateTable WHERE DATEPART( dw, ADate ) = 2) <= 7
BEGIN
INSERT INTO #DateTable
SELECT #CurrentDate
SET #CurrentDate = DATEADD( dd, -1, #CurrentDate )
END
SELECT "start_of_week" = cast(datepart(dd,dateadd(week, datediff(week, 0, ADate), 0)) as CHAR(2))
+'/'+cast(datepart(mm,dateadd(week, datediff(week, 0, ADate), 0)) as CHAR(2))
FROM #DateTable
WHERE DATEPART( dw, ADate ) = 2
DELETE #DateTable
OUTPUT
start_of_week
28/12
21/12
14/12
7 /12
30/11
23/11
16/11
9 /11
Syntax :
select "start_of_week" =
cast(datepart(dd,dateadd(week, datediff(week, 0, getdate()) - X, 0)) as CHAR(2))
select "start_of_week" =
cast(datepart(dd,dateadd(week, datediff(week, 0, getdate()) - 0, 0)) as CHAR(2)) ,
"previous_week1" =
+'/'+cast(datepart(mm,dateadd(week, datediff(week, 0, getdate()) - 1, 0)) as CHAR(2)),
"previous_week2" =
+'/'+cast(datepart(mm,dateadd(week, datediff(week, 0, getdate()) - 2, 0)) as CHAR(2)),
"previous_week3" =
+'/'+cast(datepart(mm,dateadd(week, datediff(week, 0, getdate()) - 3, 0)) as CHAR(2));
and so on.... thank you
Assuming sys.all_objects has at least 8 rows and you want the first day of the week (which you did not specify in your question:
select top 8 convert(varchar(5),
dateadd(WEEK,
1-1* ROW_NUMBER() over(order by newid()),
dateadd(DD,
1-datepart(dw,getdate()),
getdate())),
1) as [FirstDayOfWeek]
from sys.all_objects
The convert just gives the month/day. The row number is used to give the numbers 1-8. I multiplied the row number by -1 and added 1 to get the numbers 0,-1,-2,...-7 and date added those (by day) to the first day of this week. I found the first day of this week by taking getdate and date adding the negative version of the day of the week + 1.

How to get list of 2nd and 4th Saturday dates in SQL Server?

I am almost a newbie to writing SQL queries.
In the context of SQL Server, how to get list of 2nd and 4th Saturday dates
in the year 2016?
Done as a derived table simply to show the logic but you can reduce if you prefer:
select *
from (
select d2016,
datename( weekday, d2016 ) as wkdy,
row_number( ) over ( partition by datepart( month, d2016 ), datename( weekday, d2016 ) order by d2016 ) as rn_dy_mth
from (
select dateadd( day, rn, cast( '2016-01-01' as date ) ) as d2016
from (
select row_number() over( order by object_id ) - 1 as rn
from sys.columns
) as rn
) as dy
) as dy_mth
where rn_dy_mth in ( 2, 4 )
and wkdy = 'Saturday'
order by d2016
--DEFINE LIMITS FOR DAY
DECLARE #TODATE DATETIME, #FROMDATE DATETIME
SET #FROMDATE ='2010-01-01'
SET #TODATE = '2017-12-31'
;WITH DATESEQUENCE( [DATE] ) AS
(
SELECT #FROMDATE AS [DATE]
UNION ALL
SELECT DATEADD(DAY, 1, [DATE])
FROM DATESEQUENCE
WHERE DATE < #TODATE
)
, DATESATURDAY AS
(SELECT CAST(CAST(YEAR([DATE]) AS VARCHAR)+
(CASE WHEN DATEPART(M,[DATE])<=9 THEN '0'+CAST(DATEPART(M,[DATE]) AS VARCHAR)
ELSE CAST(DATEPART(M,[DATE]) AS VARCHAR) END ) AS NUMERIC) AS MONTH_ID
,CONVERT(VARCHAR,[DATE],106) AS DAY_DESC
,UPPER(DATENAME(DW,[DATE]))AS DAY_NAME
FROM DATESEQUENCE )
,SECOND_FOURTH_SATURDAY AS
(SELECT *
,ROW_NUMBER() OVER (PARTITION BY MONTH_ID ORDER BY DAY_NAME) FALL_IN
FROM DATESATURDAY
WHERE DAY_NAME='SATURDAY')
SELECT * FROM SECOND_FOURTH_SATURDAY
WHERE FALL_IN IN(2,4)
OPTION (MAXRECURSION 10000)
You can get any Saturday of a month using the Following Query in SQL.
Here I'm Getting on Current Date, You can set your own selected date to get a Specific month Saturday
select DATEADD(dd, (14 - ##DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0,getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0)) as FirstSaturday,
DATEADD(dd,7,DATEADD(dd, (14 - ##DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) as SecondSaturday,
DATEADD(dd,14,DATEADD(dd, (14 - ##DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) as ThirdSaturday,
DATEADD(dd,21,DATEADD(dd, (14 - ##DATEFIRST - DATEPART(dw, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) % 7, DATEADD(MONTH, DATEDIFF(mm, 0, getdate()), 0))) as LastSaturday

How to get first day of the week and last day of the week in sql server 2008?

How to get the first day of the week and last day of the week when we input any one day of a week?
For example if we enter a date then the first(Monday) and last (Friday) day should be displayed.
that is if we enter 24-jan-2014 then 20-jan-2014 and 24-jan-2014 should be displayed.
Regards
Here's how you can do it:
DECLARE #yourdate date = getdate()
Select dateadd(ww, datediff(ww, 0, #yourdate), 0)
Select dateadd(ww, datediff(ww, 0, #yourdate), 4)
You set #yourdate to the date you want. The first SELECT will give you the first day and the second SELECT will give you the last date
This solves it and also wraps around year ends:
SELECT DATEADD(wk, DATEDIFF(d, 0, '01 January 2017') / 7, 0)
Try this
SELECT DATEADD(wk, DATEDIFF(wk, 6, '5/13/2005'), 6)
SELECT DATEADD(wk, DATEDIFF(wk, 5, '5/13/2005'), 5)
(Or)
Declare #Date datetime
Det #Date = '2012-04-12'
Delect #Date - DATEPART(dw, #Date) + 1 FirstDateOfWeek,
#Date + (7 - DATEPART(dw, #Date)) LastDateOfWeek
With a calendar date already loaded, group can be done like this for all the years existing in the table =)
select
Y,
M,
(Select dateadd(ww, datediff(ww, 0, dt), 0) ) wk_str_dt ,
(Select dateadd(ww, datediff(ww, 0, dt), 4) )wk_end_dt ,
dt recd_crt_dt
from [tcalendar]
where isWeekday= 1
AND DW = 2 -- only mondays
order by Y, W

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())

How to get the last month data and month to date data

Need help in writing the query to get the last month data as well as month to date data.
If today's date is Mar 23 2011, I need to retrieve the data from last month and the data till todays date(means Mar 23 2011).
If date is Apr 3 2011, data should consists of March month data and the data till Apr 3rd 2011.
Thanks,
Shahsra
Today including time info : getdate()
Today without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 0)
Tomorrow without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Beginning of current month : DATEADD(month, datediff(month, 0, getdate()), 0)
Beginning of last month : DATEADD(month, datediff(month, 0, getdate())-1, 0)
so most likely
WHERE dateColumn >= DATEADD(month, datediff(month, 0, getdate())-1, 0)
AND dateColumn < DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Step back one month, subtract the number of days to the current date, and add one day.
WHERE
DateField <= GetDate() AND
DateField >= DateAdd(
mm,
-1,
DateAdd(dd, -1*DatePart(dd, GetDate())+1, GetDate())
)
To remove the time quickly, you can use this
Cast( Floor( Cast( GETDATE() AS FLOAT ) ) AS DATETIME )
So the second part would be (without time)
DateField >= Cast( Floor( Cast( (DateAdd(
mm,
-1,
DateAdd(dd, -1*DatePart(dd, GetDate())+1, GetDate())
)) AS FLOAT ) ) AS DATETIME )
Select Column1, Column2 From Table1
Where DateColumn <= GetDate() AND
DateColumn >= DATEADD(dd, - (DAY(DATEADD(mm, 1, GetDate())) - 1), DATEADD(mm, - 1, GetDate()))
Edit: +1 to Russel Steen. I was posting mine before I knew he had posted.
Very helpful page
declare #d datetime = '2011-04-03';
declare #startDate datetime;
select #startDate =
CAST('01 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,#d),113),8) AS datetime);
select #startDate;