Sunday, 14 June 2020

[SQL Server] EOMONTH - the last day of the month

[SQL Server] EOMONTH  - the last day of the month

From SQL Server 2012, EOMENTH system function can be used for getting the last day of the month containing a specified date.

Before SQL Server 2012
Script
-- Before SQL Server 2012.
DECLARE @DT VARCHAR(10);
SET @DT = '2019-02-20';

SELECT DATEADD(d, -1, DATEADD(m, 1, CONVERT(DATE, LEFT(@DT, 8) + '01'))) AS 'the last day of the month', 
       CONVERT(DATE, CONVERT(CHAR(8), @DT) + '01') AS 'the first day of the month'
GO

Result



Using EOMONTH
Script
-- Using EOMONTH Function
DECLARE @DT VARCHAR(10);
SET @DT = '2019-02-20';

SELECT EOMONTH(@DT) AS 'the last day of the month', 
       CONVERT(DATE, CONVERT(CHAR(8), @DT) + '01') AS 'the first day of the month'
GO

Result




References


No comments:

Post a Comment

(KOR)☁️ AWS Solutions Architect Associate 자격증 취득 여정

  !저의 경험 , 팁 , 그리고 SAA-C03 자격증 합격을 위한 노하우 공유! 조금 늦은 포스팅이지만 , 꼭 공유하고 싶었던 이야기입니다 . 회사 내 주변 동료들이 자주 이렇게 말하곤 했습니다 . “ 님 실...