Home > Database > Mysql Tutorial > body text

查询给定日期所在月的第一个星期一是哪天 sql语句

WBOY
Release: 2016-06-07 17:47:42
Original
1415 people have browsed it

查询给定日期所在月的第一个星期一是哪天 sql语句

查询给定日期所在月的第一个星期一是哪天 sql语句

declare @date datetime;
set @date = getdate()
--思路,1900-01-01(星期一)加上(给定日志所在月的月6号与1900-01-01差的周数)个周
--为什么不选7号?如果是7号,那么7好恰好是星期日的话,第一个周一就会算到8号。
--为什么不选5号?如果5号是星期六,那么周一就跑到上月了。小于5号与这个道理一样。
select dateadd(week,datediff(week,0,dateadd(day,6-datepart(day,@date),@date)),0) '所在月的第一个星期一'
select dateadd(week,datediff(week,0,dateadd(day,6-datepart(day,@date),@date)),7) '所在月的第二个星期一'
select dateadd(week,datediff(week,0,dateadd(day,6-datepart(day,@date),@date)),1) '所在月的第一个星期二'
select dateadd(week,datediff(week,0,dateadd(day,6-datepart(day,@date),@date)),8) '所在月的第二个星期二'
go
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template