How to get first and last day of previous month (with timestamp) in SQL Server(如何在 SQL Server 中获取上个月的第一天和最后一天(带时间戳))
问题描述
我找不到给出上个月第一天和最后一天带时间戳的解决方案.希望这对其他人有帮助.如果已经有针对此问题的解决方案,我深表歉意.
I could not find the solution which gives first and last day of previous month with timestamp. Hope this helps others. If there is already a solution for this problem I apologize.
这是解决方案.
SELECT DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0) as FirtDayPreviousMonthWithTimeStamp,
DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) as LastDayPreviousMonthWithTimeStamp
如果 currentdate = '2012-7-31'
结果:2012-06-01 00:00:00.000 2012-06-30 23:59:59.000
如果 currentdate = '2012-1-1'
结果:2011-12-01 00:00:00.000 2011-12-31 23:59:59.000
推荐答案
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --First day of previous month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) --Last Day of previous month
这篇关于如何在 SQL Server 中获取上个月的第一天和最后一天(带时间戳)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 SQL Server 中获取上个月的第一天和最后一天(带时间戳)
基础教程推荐
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
