Saturday, October 31, 2015

How to find (get) the Last day of Previous, Current and Next month in Sql server

In this article I am going to explain how to find (get) the Last day of Previous, Current and Next month in Sql server


Implementation:
Use/write the below given quires to get the last day.

Find the last day of previous month:
select CONVERT(varchar,dateadd(d,-(day(getdate())),getdate()),106)'Last Day of Previous Month'

Find the last day of current month:
select CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()))),dateadd(m,1,getdate())),106)'Last Day of Current Month'

Find the last day of next month:
select CONVERT(varchar,dateadd(d,-
(day(dateadd(m,2,getdate()))),DATEADD(m,2,getdate())),106)'Last Day of Next Month'

Here is the result:


How to find (get) the Last day of Previous, Current and Next month in Sql server

To find the day of any given/specified day follow the below given script:

DECLARE @DATE DATETIME
SET @DATE='1 jul 2014'

SELECT CONVERT(VARCHAR(25),DATEADD(d,-(DAY(@DATE)),@DATE),106) as 'Last Day of Previous Month'

SELECT CONVERT(VARCHAR(25), DATEADD(d,-(DAY(DATEADD(m,1,@DATE))),DATEADD(m,1,@DATE)),106) as 'Last Day of Current Month'

SELECT CONVERT(VARCHAR(10),DATEADD(d,-2,DATEADD(MONTH, DATEDIFF(MONTH,0,@DATE)+2,0)),106) as 'Last Day of Next Month'

 See the result:
How to find (get) the Last day of Previous, Current and Next month in Sql server


 In this article we have learn How to find the last day of previous, current and next month in sql server. I hope you enjoyed this article. 

No comments:

Post a Comment