如何使用sql获取上个月的第一天和最后一天 [英] how to get the first day and the last of previous month using sql

查看:104
本文介绍了如何使用sql获取上个月的第一天和最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到上个月的第一天,比如 01/01/2013,我也想得到上个月的最后一天,比如 01/31/2013.如果我们在三月,那么我想做同样的事情,比如 02/01/2013 和 02/28/2013 等等......谢谢

I am trying to get the first day of last month like 01/01/2013, also i want to get the last day of previous month like 01/31/2013. If we are in March then i want to do the same thing like 02/01/2013 and 02/28/2013 and so on.... thanks

推荐答案

应该这样做:

--First day of last month
SELECT DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0))
--Last day of last month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))

这篇关于如何使用sql获取上个月的第一天和最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆