'Is it possible to give the DATEADD function a variable as argument that stores either DAY, WEEK, YEAR, etc.?
What I'm trying to do is declare a variable INTERVALSTART which is the start of an interval of a query. This variable is based on the current date (INTERVALEND), reduced by the desired number of weeks, months, years, etc. I want to input this variable into the DATEADD function as an argument.
An example: I would like to run the query with the interval of the last three weeks. The query then 'gets' the information I would like it to return.
DECLARE @PERIOD_TYPE VARCHAR = 'WEEK'
DECLARE @PERIOD INT = 2
DECLARE @INTERVALEND DATE = GETDATE()
DECLARE @INTERVALSTART DATE = DATEADD(@PERIOD_TYPE, -(@PERIOD), @INTERVALEND)
The code above does not work, but it is what I would like to achieve. DATEADD does not work when I give it a VARCHAR as argument.
Is it possible? Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
