Question

I'm creating a function were I provide 3 inputs @FiscalYEar, @StartDate, @EndDate, I also declare a DATE parameter that the year will be -1 of @FiscalYear

SET @fyLowerBound = OCT 1 OF (@FiscalYear - 1)

how do I properly write the SET statement to make it work?

Was it helpful?

Solution

This should work:

DECLARE @FiscalYear INT = 2014,
        @fyLowerBound DATE;

SET @fyLowerBound = CAST(CAST((@FiscalYear - 1) AS CHAR(4)) + '1001' AS DATE)

SELECT  @fyLowerBound;

This gives 1st October 2013.

The premise being creating a string date in the format yyyyMMdd, in SQL Server this is the only culture insensitive date for DATETIME (yyyy-MM-dd will work for DATE), you then cast that string to a date (or datetime whatever your preference).

So the first step is to turn your integer date into a CHAR(4), you can then create october 1st of this year by concatenating '1001'. You now have a string that will be cast to a date.

OTHER TIPS

SET @fyLowerBound = DATEADD(yy, -1, @FiscalYear)

This will give you a date that's a year less than @FiscalYear. Although I'm not entirely sure this is what you need, given that 'OCT' in your original statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top