SQL server 2012 如何取上个月的最后一天

好吧 QQ群里被问到这种问题,还是这里写一下吧。

DECLARE @date DATETIME = getdate();

SELECT EOMONTH (@date) AS 'Last Day Of This Month',

EOMONTH (@date, 1) AS 'Last Day Of Next Month',

EOMONTH (@date, -1) AS 'Last Day Of Previous Month',

EOMONTH (@date, -2) AS 'LAST Last Day Of Previous Month'

     ;

特殊的29日也可以哦
DECLARE @date DATETIME = '2000-03-01';

SELECT EOMONTH (@date) AS 'Last Day Of This Month',

EOMONTH (@date, 1) AS 'Last Day Of Next Month',

EOMONTH (@date, -1) AS 'Last Day Of Previous Month',

EOMONTH (@date, -2) AS 'LAST Last Day Of Previous Month'

     ;

 

其实一个很闲的印度老外已经Code Prject 上写了给很全各个版本都有的表格了。。。您们慢参考我就不重复劳动了。

http://www.codeproject.com/Articles/566542/Date-and-Time-Data-Types-and-Functions-SQL-Server#29

好吧 SQL server 2008 中没有EOMONTH函数 那只能用dateadd拼接的方法来做

DECLARE @date DATETIME = '2000-03-02';

select dateadd(dd,-day(@date),dateadd(m,1,@date)) AS 'Last Day Of This Month',

         dateadd(dd,-day(@date),dateadd(m,2,@date)) AS 'Last Day Of Next Month',

         dateadd(dd,-day(@date),dateadd(m,0,@date)) AS 'Last Day Of Previous Month',

         dateadd(dd,-day(@date),dateadd(m,-1,@date)) AS 'LAST Last Day Of Previous Monthh'

给懒人

posted @ 2013-09-24 15:43  Ling.Qiu.泠  阅读(941)  评论(0编辑  收藏  举报