本文介绍了如何找到每个月的最后一个星期五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请使用SQL
Hi,
Please help me to find the last friday of each month using SQL
推荐答案
declare @year int
set @year =2011
-- First and Last Friday
select min(dates) as [First Friday], max(dates) as [Last Friday] from
(
select dateadd(day,number-1,DATEADD(year,@year-1900,0))
as dates from master..spt_values
where type='p' and number between 1 and
DATEDIFF(day,DATEADD(year,@year-1900,0),DATEADD(year,@year-1900+1,0))
) as t
where DATENAME(weekday,dates)='friday'
group by DATEADD(month,datediff(month,0,dates),0)
最好的问候,
@iamsupergrasya
Best Regards,
@iamsupergrasya
DECLARE @dateCol datetime
SELECT @dateCol = GetDate() + 60
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 YourFridayTable
WHERE DATENAME(WeekDay,LastWeekDay) = 'Friday'
Datepart(weekday,@YourDate)=6
这篇关于如何找到每个月的最后一个星期五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!