SQL查询月初与月末时间

-- 当月第一天
select dateadd(month, datediff(month, 0, getdate()), 0)

-- 当月最后一天(思路:下月的第一天减去一天)
select dateadd(month, datediff(month, 0, dateadd(month, 1, getdate())), -1)

--取某月第一天:
SELECT CAST((LEFT('201902',4)+'-'+SUBSTRING('201902',5,LEN('201902'))+'-'+'01') AS DATETIME)

--取某月最后一天:
SELECT DATEADD(DAY,-1,DATEADD(MONTH,1,CAST((LEFT('201902',4)+'-'+SUBSTRING('201902',5,LEN('201902'))+'-'+'01') AS DATETIME)))

上一篇:stream 分组


下一篇:Java8新特性:Stream API