原文:
SQL server 2012 如何取上個月的最後一天好吧 QQ群裡被問到這種問題,還是這裡寫一下吧。
DECLARE
@date
DATETIME
=
getdate();
SELECT
EOMONTH
(@date)
AS
'Last Day Of This Month',
(@date, 1)
'Last Day Of Next Month',
(@date,
-1)
'Last Day Of Previous Month',
-2)
'LAST Last Day Of Previous Month'
;

特殊的29日也可以哦
'2000-03-01';
其實一個很閑的印度老外已經Code Prject 上寫了給很全各個版本都有的表格了。。。您們慢參考我就不重複勞動了。
http://www.codeproject.com/Articles/566542/Date-and-Time-Data-Types-and-Functions-SQL-Server#29好吧 SQL server 2008 中沒有EOMONTH函數 那隻能用dateadd拼接的方法來做
'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))
'Last Day Of Next Month',
dateadd(dd,-day(@date),dateadd(m,0,@date))
'Last Day Of Previous Month',
dateadd(dd,-day(@date),dateadd(m,-1,@date))
'LAST Last Day Of Previous Monthh'
給懶人