sql 如何找到一个月的最后一天

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 如何找到一个月的最后一天相关的知识,希望对你有一定的参考价值。


---------------------------------SQL Server-------------------------------------------------------------------------

select

convert(date,getdate()) as Today,
eomonth(getdate(),-1)   as LastDayPreviousMonth,
eomonth(getdate(),+0)   as LastDayThisMonth,
eomonth(getdate(),+1)   as LastDayNextMonth


Today      |LastDayPreviousMonth |LastDayThisMonth |LastDayNextMonth |
-----------|---------------------|-----------------|-----------------|
2017-11-18 |2017-10-31           |2017-11-30       |2017-12-31       |

---------------------------------Vertica----------------------------------------------------------------------------

select 

now()                          as Today
last_day(Now())                as LastDayofCurrentMonth,     
last_day(add_months(Now(),-1)) as LastDayofPreviousMonth,     
last_day(add_months(Now(), 1)) as LastDayofNextMonth           


Today      |LastDayPreviousMonth |LastDayThisMonth |LastDayNextMonth |
-----------|---------------------|-----------------|-----------------|
2017-11-18 |2017-10-31           |2017-11-30       |2017-12-31       |


#--------------------------------MySQL-------------------------------------------------------------------------------

select

date(now())                   as Today,
last_day(adddate(Now(), -31)) as LastDayPreviousMonth,
last_day(Now())               as LastDayThisMonth,
last_day(adddate(Now(), +31)) as LastDayNextMonth


Today      |LastDayPreviousMonth |LastDayThisMonth |LastDayNextMonth |
-----------|---------------------|-----------------|-----------------|
2017-11-18 |2017-10-31           |2017-11-30       |2017-12-31       |


---------------------------------PostgreSQL----------------------------------------------------------------------------

select

Date(Now()) as Today,
Date(date_trunc('month', Now() + interval '0 month') - interval '1 day')  as LastDayPreviousMonth,
Date(date_trunc('month', Now() + interval '1 month') - interval '1 day')  as LastDayThisMonth,
Date(date_trunc('month', Now() + interval '2 month') - interval '1 day')  as LastDayNextMonth


today      |lastdaypreviousmonth |lastdaythismonth |lastdaynextmonth |
-----------|---------------------|-----------------|-----------------|
2017-11-18 |2017-10-31           |2017-11-30       |2017-12-31       |


以上是关于sql 如何找到一个月的最后一天的主要内容,如果未能解决你的问题,请参考以下文章

在 SQL 中获取当月的最后一天

我如何获得一个月的最后一天?

用SQL取每个月的最后一天,怎么写

如何在n个月内使用sql(无程序)获取每个月的第一天和最后一天

sql 语句 怎么获取指定月的第一天和最后一天?

如何在 DB2 中获取每个月的当前最后一天