Get 1st April

  • Hey,

    I need to find a nice neat way of setting a variable to 1st April of that current 'contract year' based on today's date.

    Today's Date Contract Start Date

    2016-04-21 > 2016-04-01

    2016-01-12 > 2015-04-01

    2018-05-01 > 2018-04-01

    2016-01-01 > 2015-04-01 (my script give me 2016-04-01)

    I want to use GETDATE() so that it can be ran automatically.

    DECLARE @STARTDATE_CURRENT VARCHAR (20)

    SET @STARTDATE_CURRENT = '2016-01-01'

    DECLARE @CONTRACT_STARTDATE VARCHAR (20)

    SET @CONTRACT_STARTDATE = CASE

    WHEN Substring(@STARTDATE_CURRENT, 5, 2) IN ( '01',

    '02', '03' ) THEN Cast(

    Year(Cast(Substring(@STARTDATE_CURRENT, 1, 4)AS DATE

    ))-1 AS VARCHAR)

    + '0401'

    ELSE Cast(Year(Cast(Substring(@STARTDATE_CURRENT, 1,

    4)AS DATE)) AS VARCHAR)

    + '0401'

    END

    PRINT @CONTRACT_STARTDATE

    Thanks in advance for any help.

  • Try this

    DECLARE @CurrentDate DATETIME = GETDATE() --'2016-01-01'

    select DATEADD(qq,1,DATEADD(yy,DATEDIFF(yy,0,DATEADD(qq,-1,@CurrentDate)),0))

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank you very much GilaMonster! It worked perfectly.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply