sql-server - SQL Server 中的周数
问题描述
下面的查询创建星期天到星期六
但我需要从周一到周日的几周
declare @begDate datetime
declare @endDate datetime
set @begDate = '01-APR-2020'
set @endDate = '30-APR-2020';
WITH N(n) AS
(
SELECT 0
UNION ALL
SELECT n+1
FROM N
WHERE n <= datepart(dd,@enddate)
)
SELECT
DATEADD(dd, n, @BegDate) AS dDate
INTO
#Noofweeks
FROM
N
WHERE
MONTH(DATEADD(dd, n, @BegDate)) = MONTH(@BegDate)
SELECT
MIN(dDate) AS BegOfWeek,
MAX(dDate) AS EndOfWeek,
DATEDIFF(week, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, datediff(month, 0, dDate), 0)), 0), dDate) AS WeekNumForMonth
FROM
#Noofweeks
GROUP BY
DATEDIFF(week, DATEADD(week, DATEDIFF(week, 0, DATEADD(month, datediff(month, 0, dDate), 0)), 0), dDate)
ORDER BY
3, 1
DROP TABLE #Noofweeks
解决方案
您可以设置一周的第一天:
DECLARE @datefirst int=@@DATEFIRST;
SET DATEFIRST 1;
--- Your code here
---
SET DATEFIRST @datefirst;
推荐阅读
- griddb - 刚在 GridDB 的表中添加 myName 时找不到 myName
- r - 从对象中提取特定字符串
- angular - 使用 Input() 的组件之间的角度传递数据不会触发 ngOnChanges
- java - 如何将规则脚本注入 java.sql.Statement.executeQuery(String)?
- android - 安卓。画布吹动画
- go - go 如何在 map 和 slice 等内置类型中实现 Generic?
- tensorflow - 尝试在 tf.distribute.Strategy 的范围内创建优化器槽变量,该范围与用于原始变量的范围不同
- powerbi - 动态下拉切片器列表
- sqlite - 将带有 sqlite 的烧瓶部署到 heroku
- python - 反复询问现有文件