Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Getting last Friday of the month

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-11-18 : 14:31:18
How would I get this? I have a field in a table called weekdat. That field has all Friday's dates of each month in there. How do I just get the last Friday of each month?

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-11-18 : 14:41:43
Never mind I got it:

SELECT DISTINCT CONVERT(char, weekdat, 101) AS FridayDte
FROM Text
ORDER BY FridayDte
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-11-18 : 15:34:40
Okay I'm back I am getting all of the Friday's in the month. How do I just get the last Friday?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-18 : 16:27:41
Maybe something in here will help

http://weblogs.sqlteam.com/brettk/archive/2005/06/02/5528.aspx



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-18 : 16:28:17
[code]
SELECT DatePart(year, weekdat) as [year], DatePart(month, weekdat) as [month], Max(weekdat) as LastFridayInMonth
FROM YourTable
Group BY
DatePart(year, weekdat), DatePart(month, weekdat)
ORDER BY
LastFridayInMonth
[/code]
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-11-19 : 10:03:16
Thanks so much Russell for this it works!

Thanks Brett for the link I will keep this as well. Some good stuff is on there!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 10:18:26
ummmm...That's not what I get with the code


SELECT DATEPART(year, weekdat) AS [year]
, DATEPART(month, weekdat) AS [month], MAX(weekdat) AS LastFridayInMonth
FROM (SELECT GetDate() AS weekdat) AS YourTable
GROUP BY DATEPART(year, weekdat), DATEPART(month, weekdat)
ORDER BY LastFridayInMonth


Gives me


year month LastFridayInMonth
----------- ----------- -----------------------
2009 11 2009-11-19 10:19:14.873

(1 row(s) affected)




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-11-19 : 10:34:28
select weekdat
from
(

select [Col] = dateadd(month,datediff(month,0,weekdat),0)
,weekdat
,[Rank] = rank() over(partition by dateadd(month,datediff(month,0,weekdat),0) order by weekdat desc)
from yourTable
) t1



where rank = 1


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 10:43:00
hmmm...gotta try that...I was playing with



DECLARE @dateCol datetime
SELECT @dateCol = GetDate()

SELECT LastWeekDay
FROM (
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-1 AS LastWeekDay
UNION ALL
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-2 AS LastWeekDay
UNION ALL
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-3 AS LastWeekDay
UNION ALL
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-4 AS LastWeekDay
UNION ALL
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-5 AS LastWeekDay
UNION ALL
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-6 AS LastWeekDay
UNION ALL
SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-7 AS LastWeekDay
) AS XXX
WHERE DATENAME(WeekDay,LastWeekDay) = 'Friday'




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 10:46:55
Nope....still gives the wrong Friday



SELECT weekdat
FROM (
SELECT [Col] = dateadd(month,datediff(month,0,weekdat),0)
, weekdat
, [Rank] = rank() OVER(
PARTITION BY DATEADD(month,DATEDIFF(month,0,weekdat),0)
ORDER BY weekdat DESC)
FROM (SELECT GetDate() AS weekdat) AS YourTable
) t1
WHERE rank = 1





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-11-19 : 10:53:55
Supposedly all he has in his tables are Fridays, he just wants the last one from each month. Won't my solution give him that?

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 10:55:46
quote:
Originally posted by jimf

Supposedly all he has in his tables are Fridays, he just wants the last one from each month. Won't my solution give him that?

Jim

Everyday I learn something that somebody else already knew



OK, Didn't catch that

I proposed a solution to find the ACTUAL Last Friday of the Month



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-11-19 : 10:58:29
http://weblogs.sqlteam.com/peterl/archive/2009/06/17/How-to-get-the-Nth-weekday-of-a-month.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-19 : 11:09:29
quote:
Originally posted by X002548

ummmm...That's not what I get with the code


works based on what OP says. is better just to use pure date math though
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 11:21:01
quote:
Originally posted by khtan

http://weblogs.sqlteam.com/peterl/archive/2009/06/17/How-to-get-the-Nth-weekday-of-a-month.aspx


KH
[spoiler]Time is always against us[/spoiler]





Well that get's her done

Way to go Peso

http://www.sqlteam.com/forums/pop_profile.asp?mode=display&id=22680


CREATE FUNCTION dbo.fnGetNthWeekdayOfMonth
(
@theDate DATETIME,
@theWeekday TINYINT,
@theNth SMALLINT
)
RETURNS DATETIME
BEGIN
RETURN (
SELECT theDate
FROM (
SELECT DATEADD(DAY, 7 * @theNth - 7 * SIGN(SIGN(@theNth) + 1) +(@theWeekday + 6 - DATEDIFF(DAY, '17530101', DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) % 7) % 7, DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) AS theDate
WHERE @theWeekday BETWEEN 1 AND 7
AND @theNth IN (-5, -4, -3, -2, -1, 1, 2, 3, 4, 5)
) AS d
WHERE DATEDIFF(MONTH, theDate, @theDate) = 0
)
END
GO


SELECT dbo.fnGetNthWeekdayOfMonth(GetDate(), 5, -1)
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-11-19 : 12:47:26
Wow you guys are still at it. Well that's great thanks for all of your help I'm been trying to add Russell's info to my pivot query but can't get it to work.

How do I add this (thanks Russell)
SELECT	DatePart(year, weekdat) as [year], DatePart(month, weekdat) as [month], Max(weekdat) as LastFridayInMonth
FROM DiaryPct
Group BY
DatePart(year, weekdat), DatePart(month, weekdat)
ORDER BY
LastFridayInMonth


to this code?


DECLARE @listCol VARCHAR(2000)

SELECT @listCol = STUFF(( SELECT DISTINCT

'],[' + convert(varchar(10),(Weekdat),1) --converting the weekdat

FROM DiaryPct

--where (dateadd(ww, -14, getdate()) < weekdat) --getting 13 weeks of data out

ORDER BY '],[' + convert(varchar(10),(weekdat),1)

FOR XML PATH('')

), 1, 2, '') + ']';
DECLARE @sql NVARCHAR(2000);
SET @sql = N'
WITH Totals AS (
SELECT sort, doc, dist, area,CASE
WHEN SUM(totovr)= 0 THEN 0
WHEN SUM(totpnd) = 0 THEN 0
ELSE
(convert(decimal(6,1),(((((sum(totovr))) * 1.00) / (sum(totpnd))) * 100)))
END AS total, weekdat
FROM DiaryPct
GROUP BY sort,doc, dist, area,weekdat)
SELECT sort,doc, dist, area, ' + @listCol +
N' Into testPivot ' +
N'FROM
(SELECT sort,doc, dist, area,total, weekdat
FROM Totals) as O ' +
N' PIVOT ' +
N'(SUM(total) FOR weekdat IN (' + @listCol + N')) AS P;';


EXEC(@sql);
Go to Top of Page
   

- Advertisement -