TSQL 计算当月的周数 [英] TSQL Calculate week number of the month

查看:56
本文介绍了TSQL 计算当月的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算当月的周数,我想计算周数,无论是奇数周还是偶数周我怎样才能在 TSQL 中得到这个?谢谢大家!

I want to calculate the week number of the month,I want to calculate the week number whether its odd or even week how can I get this in TSQL ? Thanks all!

推荐答案

这会为您提供日期 @dt 在其月份中的第几周.第二列在表达式上使用 CASE 语句,以显示奇数"或偶数"

This gives you the week of the date @dt within its month. There is a 2nd column that uses a CASE statement over the expression, to show either "Odd" or "Even"

declare @dt datetime
set @dt = GETDATE()

select
    WhichWeekOfMonth = datepart(wk, @dt)
                     - datepart(wk,dateadd(m, DATEDIFF(M, 0, @dt), 0)) + 1,
    case when (datepart(wk, @dt)
            - datepart(wk,dateadd(m, DATEDIFF(M, 0, @dt), 0)) + 1) % 2 = 1
         then 'Odd' else 'Even' end

这篇关于TSQL 计算当月的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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