Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
how to get last date of month and week number

Answered how to get last date of month and week number

  • Tuesday, September 25, 2012 5:15 AM
     
     

    Hi,

       I have 10 years of dates in one column X I want month end date in column Y , week number in column Z of that  related to each date in column X .

    • Moved by Tom PhillipsModerator Tuesday, September 25, 2012 2:17 PM TSQL question (From:SQL Server Database Engine)
    •  

All Replies

  • Tuesday, September 25, 2012 5:24 AM
     
     Answered Has Code

    Try the below:

    Create Table T11(Col1 Datetime) Insert into T11 Select GETDATE() SELECT Col1,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,Col1)+1,0)), DATEPART( wk, Col1 ) From T11



    Please click the Mark as Answer or Vote As Helpful button if a post solves your problem or is helpful!

    • Proposed As Answer by vr.babu Tuesday, September 25, 2012 5:35 AM
    • Marked As Answer by Iric WenModerator Wednesday, October 03, 2012 8:15 AM
    •  
  • Friday, September 28, 2012 11:51 PM
    Moderator
     
     Answered Has Code

    SQL Server 2012 has the EOMONTH() function:

    -- Month end date
    SELECT EOMONTH(getdate());
    -- 2012-09-30
    -- Month start date
    SELECT DATEADD(DD,1,EOMONTH(getdate(),-1));
    -- 2012-09-01
    It can be used to calculate month start date also.

    Kalman Toth SQL SERVER 2012 & BI TRAINING
    New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012

  • Saturday, September 29, 2012 4:22 AM
     
      Has Code
    Declare @Year Int, @Month Int,@Day Int
    
    Select @Year = 2012,@Month = 2,@Day = 1 
    
    
    Select DATEADD(dd,-1, DateAdd(M,1,Cast(Cast(@Year As varchar(4))+'-'+
    	Cast(@Month As varchar(4))+'-01 00:00:00.001' As DateTime)))
    	As LastDateOfMonth,
    	 
    	  DATEPART(wk,CAST(Cast(@Year As Varchar(4))
    	  +'-'+Cast(@Month As Varchar(2))+'-'+Cast(@Day As Varchar(2))
    	    As DateTime)) As WeekNoOfDate
    	 


    Please, If answer match your requirement then set mark as answer