如何找到每个月的最后一个星期五 [英] How to find a last friday of each month

查看:208
本文介绍了如何找到每个月的最后一个星期五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


请使用SQL

Hi,
Please help me to find the last friday of each month using SQL

推荐答案

帮我找出每个月的最后一个星期五.

Try this one.

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


您可以从下面的查询中找到上周五,我度过了一个虚拟的日子,可以在任何需要的地方使用该表.

You can find last Friday from the below Query, i took a dummy day, you can done with the table, where ever you wants.

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'


以下是给您的提示:
-查找每月的天数.
-从每月的天数开始循环-1,直到比赛星期五.
如果使用SQL Server,则查找星期五:
Here is tips for you:
- Find number of days in month.
- Loop step -1 from number of day in month until match Friday.
If you use SQL Server, to find Friday:
Datepart(weekday,@YourDate)=6


这篇关于如何找到每个月的最后一个星期五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆