好吧 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'

     ;

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

特殊的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'

     ;

 

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

其实一个很闲的印度老外已经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'

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

给懒人

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-09-30
  • 2021-10-03
  • 2022-02-01
相关资源
相似解决方案