SQL Server有一个很强大的函数:DATEPART()

它可以计算2017-10-17是属于今年的

第几天:select datepart(dy,'2017-10-17')

第几周:select datepart(wk,'2017-10-17')

第几月:select datepart(mm,'2017-10-17')

第几季度:select datepart(qq,'2017-10-17')

一周中的星期几:select (case datepart(dw,'2017-10-17')

when 1 then 'Sunday'

when 2 then 'Monday'

when 3 then 'Tuesday'

when 4 then 'Wednesday'

when 5 then 'Thursday'

when 6 then 'Friday'

when 7 then 'Saturday'

end)

那么问题来了:如果我们需要知道2017年的第25周是哪几天,应该怎么编写SQL

分析:

1. 2017年第25周第1天是几月几号

1.1 2017年的第1周的第1天2017-1-1是星期几

1.2 每周有7天

1.3 2017-1-1需要加上多少天等于2017年第25周第1天

那么,第25周的第1天 = 2017年的第1周天数 + (25-2)*7,25-2即减去第1周和第25周

2、2017年第25周最后1天是几月几号

2017年第25周最后1天 = 2017年第25周第1天 + 6天

到了这里,我们知道关键的点为如何计算2017第1周的天数,在计算天数时最关键的是确定星期日是一周的第1天还是最后一天。

2017年1月1日恰好是星期日

如果星期日是一周的第一天

2017年第25周的第1天 = 2017年1月1日 + 7天 + (25-2)*7天

SQL语句:dateadd(day,7+(25-2)*7,'2017-1-1'),结果为:2017-6-18

2017年第25周为:2017-6-18至2017-6-24

如果星期日是一周的最后一天

2017年第25周的第1天 = 2017年1月1日 + 1天 + (25-2)*7天

SQL语句:dateadd(day,1+(25-2)*7,'2017-1-1'),结果为:2017-6-12

2017年第25周为:2017-6-12至2017-6-18

接下来把上面的计算方式编写为SQL语句

星期日为第1天:

declare @weekofyear int

declare @date datetime

declare @dayofweek int

declare @firstday varchar(20)

declare @firstweek int

set @date='2017-1-1'

select @dayofweek=datepart(dw,@date)

set @weekofyear=25

set @firstweek=8-@dayofweek

declare @begin_date datetime

declare @end_date datetime

set @begin_date=dateadd(day, @firstweek+(@weekofyear-2)*7,@date)

set @end_date=dateadd(day,6,@begin_date)

星期日为最后一天:

declare @weekofyear int

declare @date datetime

declare @dayofweek int

declare @firstweek int

set @date='2017-1-1'

select @dayofweek=datepart(dw,@date)

set @weekofyear=25

if(9-@dayofweek)>7

begin

set @firstweek=9-@dayofweek-7

end

else

begin

set @firstweek=9-@dayofweek

end

declare @begin_date datetime

declare @end_date datetime

set @begin_date=dateadd(day, @firstweek+(@weekofyear-2)*7,@date)

set @end_date=dateadd(day,6,@begin_date)

再将代码整合为存储过程,结果如下:

结果验证:

转载于:https://blog.51cto.com/aimax/1973364

如何在SQL Server计算XX年第XX周是哪几天相关推荐

  1. 如何在SQL Server Reporting Services中自动创建KPI

    关键绩效指标(KPI) (Key Performance Indicator (KPI)) A Key Performance Indicator aka KPI is a metric which ...

  2. 如何在SQL Server Management Studio中创建和配置链接服务器以连接到MySQL

    This article will guide you with all the necessary steps to successfully create a linked server in S ...

  3. 如何在SQL Server中比较表

    介绍 (Introduction) If you've been developing in SQL Server for any length of time, you've no doubt hi ...

  4. 如果不使用 SQL Mail,如何在 SQL Server 中发送电子邮件

    如果不使用 SQL Mail,如何在 SQL Server 中发送电子邮件 察看本文应用于的产品 文章编号 : 312839 最后修改 : 2006年12月21日 修订 : 10.1 本页 概要 SQ ...

  5. 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集

    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括: 如 ...

  6. 如何在SQL Server数据库中加密数据

    如何在SQL Server数据库中加密数据 为了防止某些别有用心的人从外部访问数据库,盗取数据库中的用户姓名.密码.信用卡号等其他重要信息,在我们创建数据库驱动的解决方案时,我们首先需要考虑的的第一条 ...

  7. 如何在SQL Server中实现错误处理

    错误处理概述 (Error handling overview) Error handling in SQL Server gives us control over the Transact-SQL ...

  8. sql server 数组_如何在SQL Server中实现类似数组的功能

    sql server 数组 介绍 (Introduction) I was training some Oracle DBAs in T-SQL and they asked me how to cr ...

  9. 如何在 SQL Server 2005 故障转移群集中添加或删除节点(安装程序)

    如何在 SQL Server 2005 故障转移群集中添加或删除节点(安装程序) 使用此过程管理 Microsoft SQL Server 2005 故障转移群集实例中的节点. 重要提示: 若要更新或 ...

  10. 如何在SQL Server中附加Pubs数据库

    在本教程中,我将解释如何 在SQL Server中 附加Pubs数据库  . 每个其他数据库的过程都是相同的. 您需要将Pubs MDF和LDF文件附加到SQL Server. 请注意,Northwi ...

最新文章

  1. react-webpack-express
  2. Linux主机WordPress伪静态设置方法
  3. InfluxDB基本概念和操作
  4. excel两个表格数据对比_Excel表格怎么防止看错数据?阅读模式了解一下
  5. 快速构建Windows 8风格应用22-MessageDialog
  6. SAP cross distribution chain status在Fiori应用中的draft handling
  7. 诊断日志知多少 | DiagnosticSource 在.NET上的应用
  8. javascript删除数组里的对象
  9. Question of the Day: Microsoft | Database, Multiple Questions in One
  10. php生成图片文件流,php如何将base64数据流文件转换为图片文件?
  11. Hibernate VS Mybatis 的SQL不灵活通病如何解决?
  12. 线程概念 多线程模型
  13. W-3 用grub4dos安装Windows7、Ubuntu 12.10双系统(图解)
  14. matlab中关于统计的函数
  15. llvm+clang 添加 LTO(Link Time Optimization) 支持
  16. 使用Quartz实现定时任务
  17. 【企业】全球顶级思维模式:建立思维模式,把握人性管理
  18. leetcode回溯算法
  19. 昆明新迎万枫、菏泽希尔顿花园酒店​开业;万豪在华运营酒店超过400家 | 中国酒店周刊...
  20. C/C++ 函数出入口

热门文章

  1. Oracle学习之路-- 案例分析实现行列转换的几种方式
  2. C Primer Plus(十二)
  3. 最简单的三层实例【插入据
  4. SoundMorph Dust for Mac(双耳环绕音频颗粒合成仪)
  5. NoiseAsh Rule Tec All Collection for Mac(无源均衡器)
  6. Python基础_字符串的格式化
  7. 滴滴宣布架构大调整,强调安全第一
  8. 通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理
  9. 3个框框带你理解EventLoop
  10. JavaWeb项目架构之FastDFS分布式文件系统