Home  >  Article  >  Database  >  SQLSERVER取当前月第一天和最后一天

SQLSERVER取当前月第一天和最后一天

WBOY
WBOYOriginal
2016-06-07 15:44:322534browse

--本月第一天: selectdateadd(dd,-day(getdate())+1,getdate()) --本月最后一天: SELECTdateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0)) --上个月第一天: select CONVERT(int, CONVERT(varchar(10),DATEADD(m,-1 ,dateadd(dd,-day(getdate())+1,g

--本月第一天:

select   dateadd(dd,-day(getdate())+1,getdate())   

--本月最后一天:

SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))

 

 

--上个月第一天:

select CONVERT(int, CONVERT(varchar(10),DATEADD(m,-1 ,dateadd(dd,-day(getdate())+1,getdate())) ,112))

--上个月最后一天:

SELECT CONVERT(int,convert(varchar(10),dateadd(d,-1,dateadd(m,-1,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))),112))

 

附录:http://www.w3school.com.cn/sql/sql_dates.asp 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn