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.
| 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 FridayDteFROM TextORDER BY FridayDte |
 |
|
|
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? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 LastFridayInMonthFROM YourTableGroup BY DatePart(year, weekdat), DatePart(month, weekdat)ORDER BY LastFridayInMonth[/code] |
 |
|
|
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! |
 |
|
|
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 YourTableGROUP BY DATEPART(year, weekdat), DATEPART(month, weekdat)ORDER BY LastFridayInMonth Gives meyear month LastFridayInMonth----------- ----------- -----------------------2009 11 2009-11-19 10:19:14.873(1 row(s) affected) Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-11-19 : 10:34:28
|
| select weekdatfrom( 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) t1where rank = 1JimEveryday I learn something that somebody else already knew |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-19 : 10:43:00
|
hmmm...gotta try that...I was playing withDECLARE @dateCol datetimeSELECT @dateCol = GetDate()SELECT LastWeekDay FROM (SELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-1 AS LastWeekDayUNION ALLSELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-2 AS LastWeekDayUNION ALLSELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-3 AS LastWeekDayUNION ALLSELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-4 AS LastWeekDayUNION ALLSELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-5 AS LastWeekDayUNION ALLSELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-6 AS LastWeekDayUNION ALLSELECT DATEADD(mm,1,@dateCol - DAY(@dateCol)+1)-7 AS LastWeekDay) AS XXXWHERE DATENAME(WeekDay,LastWeekDay) = 'Friday' Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-11-19 : 10:46:55
|
Nope....still gives the wrong FridaySELECT 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 Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
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?JimEveryday I learn something that somebody else already knew |
 |
|
|
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?JimEveryday I learn something that somebody else already knew
OK, Didn't catch thatI proposed a solution to find the ACTUAL Last Friday of the MonthBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
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] |
 |
|
|
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 |
 |
|
|
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 doneWay to go Pesohttp://www.sqlteam.com/forums/pop_profile.asp?mode=display&id=22680CREATE FUNCTION dbo.fnGetNthWeekdayOfMonth( @theDate DATETIME, @theWeekday TINYINT, @theNth SMALLINT)RETURNS DATETIMEBEGIN 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 )ENDGOSELECT dbo.fnGetNthWeekdayOfMonth(GetDate(), 5, -1)GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
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 LastFridayInMonthFROM DiaryPctGroup 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 DiaryPctGROUP 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); |
 |
|
|
|
|
|
|
|