Home >Database >Mysql Tutorial >sql 计算给定日期所在月与所在季度的天数的sql语句

sql 计算给定日期所在月与所在季度的天数的sql语句

WBOY
WBOYOriginal
2016-06-07 17:47:381658browse

sql 计算给定日期所在月与所在季度的天数的sql语句

sql 计算给定日期所在月与所在季度的天数的sql语句

declare @date datetime;
set @date = getdate()
--本月度第一天与下月度第一天所差的天数
select datediff(day,dateadd(month,datediff(month,0,@date),0),dateadd(month,1+datediff(month,0,@date),0))
--借助变量简化
select @date = dateadd(month,datediff(month,0,@date),0) --本月度第一天
select datediff(day,@date,dateadd(month,1,@date))
--另一种思路:给定月最后一天的日期,记为本月天数
select day(dateadd(month,datediff(month,-1,@date),-1))
go

--计算给定日期所在季度的天数

declare @date datetime;
set @date = getdate()
--本季度第一天与下季度第一天所差的天数
select datediff(day,dateadd(quarter,datediff(quarter,0,@date),0),dateadd(quarter,1+datediff(quarter,0,@date),0))
--借助变量简化
select @date = dateadd(quarter,datediff(quarter,0,@date),0) --本季度第一天
select datediff(day,@date,dateadd(quarter,1,@date))
go
Statement:
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