How to get the last day of the quarter

blackhills 136 Reputation points
2020-12-01T21:01:46.643+00:00

Hi All,
I have a CALENDAR_DATE , based on this date i want to create the LAST_DAY_OF_THE_QUARTER_IND ,please advice,
Thanks

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,657 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 112.7K Reputation points MVP
    2020-12-01T22:18:45.787+00:00
    DECLARE @date date = '20201202'
    SELECT convert(date, 
             datename(year, @date) + 
               CASE datepart(quarter, @date)
                  WHEN 1 THEN '0430'
                  WHEN 2 THEN '0630'
                  WHEN 3 THEN '0930'
                  WHEN 4 THEN '1231'
               END)
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2020-12-01T22:59:25.423+00:00
    DECLARE @date date = '2020-12-01';
    SELECT DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, @date) +1, 0));
    
    0 comments No comments

  2. EchoLiu-MSFT 14,581 Reputation points
    2020-12-02T01:45:34.403+00:00

    Hi @blackhills ,

    Please refer to:

    DECLARE @TB TABLE(TIME DATETIME)  
    INSERT INTO @TB VALUES('2013-02-09 11:22:59.997')  
    INSERT INTO @TB VALUES('2013-05-07 13:12:51.333')  
    INSERT INTO @TB VALUES('2013-07-07 15:42:22.633')  
    INSERT INTO @TB VALUES('2013-10-20 19:29:22.783')   
    SELECT *,CASE WHEN DATEPART(QQ,TIME)=1 THEN DATEADD(D,-1,'2013-04-01 23:59:59.997')       
                  WHEN DATEPART(QQ,TIME)=2  THEN DATEADD(D,-1,'2013-07-01 23:59:59.997')        
                  WHEN DATEPART(QQ,TIME)=3  THEN DATEADD(D,-1,'2013-10-01 23:59:59.997')         
                  WHEN DATEPART(QQ,TIME)=4  THEN DATEADD(D,-1,'2014-01-01 23:59:59.997')  END AS QUARTER             
    FROM @TB  
    

    44207-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues November--What can I do if my transaction log is full?
    Hot issues November--How to convert Profiler trace into a SQL Server table

    0 comments No comments

  3. blackhills 136 Reputation points
    2020-12-03T14:13:23.367+00:00

    Thanks All,
    All the scenarios worked fine, but it will only let me select only one as Answer. Thanks again,

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.