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) AI와 지속 가능한 엔지니어링 — 생성은 빠르게, 검증은 철저하게

영어 원문 : https://www.linkedin.com/pulse/ai-sustainable-engineering-generate-fast-verify-thoroughly-yoon-hclqf/ [공지 / 면책 조항] 이 글에 표현된 모든 견해는 전...