如何在SQL中获取一个月的周数 [英] How to get the week number of a month in SQL

查看:1066
本文介绍了如何在SQL中获取一个月的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一年的周数可以使用Datepart函数计算,同样如何计算一个月的周数。



我尝试了什么:



Week number of a year can be calculated using Datepart function, likewise how to calculate week number of a month.

What I have tried:

SELECT  DATEPART(WEEK,GETDATE())

- 一年的周数

推荐答案

没有特定的一个月的周数功能 - 事实上,我从未在任何框架中看到过。大多数情况下我怀疑是因为没有关于一个月的周数的标准定义(有一年中的一周,它取决于第一个星期四的日期:ISO周日期 - 维基百科 [ ^ ])但几个月没有相应的,这将有相同的问题。三月的第一天是3月的第1周,还是2月的第3周,第4周或第5周? 2月29日是2月4日或5日,还是3月1日?没有标准定义,因此没有标准功能。



您必须自己决定规则,然后实施它们。祝你好运 - 这将是一个有趣的项目......
There is no specific "week number of a month" function - indeed, I've never seen one in any framework. Mostly I suspect because there is no standard definition of what constitutes a week number for a month (there is for "week of year", and it depends on the date of the first Thursday: ISO week date - Wikipedia[^]) but there is no equivalent for months, which will have the same problems. Is the first of March in week 1 of March, or week 3, 4, or 5 of February? Is the 29th February in week 4 or 5 of February, or week 1 of March? There is no standard definition, so no standard function.

You will have to decide the rules yourself, and then implement them. Good luck - it'll be a fun project...


检查一下:



Check this:

declare @date datetime = '2012-09-01'
select datepart(day, datediff(day, 0, @date)/7 * 7)/7 + 1





来自: tsql - 如何从sql server 2008中的日期获得本月的周数 [ ^ ]


这篇关于如何在SQL中获取一个月的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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