Click here to Skip to main content
16,006,378 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
how to get last date of the month from the current date in sql server
Posted
Comments
CHill60 28-May-14 3:30am    
Just for the record, typing your question into Google brought up 5 million hits. The very first one would have solved your problem far quicker than typing this question in here

SQL
CREATE FUNCTION [dbo].[udf_GetLastDayOfMonth]
(
    @Date DATETIME
)
RETURNS DATETIME
AS
BEGIN

    RETURN DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @Date) + 1, 0))

END


Query:

SQL
DECLARE @date DATETIME
SELECT @date = '2013-05-31 15:04:10.027'

SELECT DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @date) + 1, 0))


Output:

-----------------------
2013-05-31 00:00:00.000


or if you have sql server 2012..

SQL
SELECT CAST(eomonth(GETDATE()) AS datetime)



Please accept if its work for you..
 
Share this answer
 
v2
Comments
CHill60 28-May-14 3:47am    
Gets my 5 for the eomonth!
Animesh Datta 28-May-14 6:58am    
my 5!
SQL
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),111)

Change the 111 if you want a different date format
This produces
Quote:
2014/05/31
 
Share this answer
 
Comments
Awadhendra Tripathi 28-May-14 3:46am    
That is Good !!!!
Refer this

DECLARE @Month INTEGER
DECLARE @Year INTEGER
SET @Month = MONTH(GETDATE())
SET @Year = YEAR(GETDATE())

DECLARE @FirstDayOfMonth DATETIME
DECLARE @LastDayOfMonth DATETIME

SET @FirstDayOfMonth = Str(@Year) + RIGHT('0' + Str(@Month), 2) + '01'
SET @LastDayOfMonth = DATEADD(dd, -1, DATEADD(mm, 1, @FirstDayOfMOnth))
SELECT @FirstDayOfMonth, @LastDayOfMonth
 
Share this answer
 
Comments
CHill60 28-May-14 3:37am    
Produces exception when run
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900