Assign default value 6 months in the past to datetime variable on data.stackexchange.com

54 Views Asked by At

I found a query on data.stackexchange.com (Interesting unanswered questions) and wanted to make a variation with an optional datetime variable to avoid getting questions that are very old in the results.

I hard coded a date six months in the past DECLARE @FromDate datetime = ##FromDate?2018-06-01## which works okay for now, But I would like to future-proof it.

Is it possible to assign a default date which is dynamically six months in the past in a not-too-round about way?

Interesting unanswered questions with date limit

1

There are 1 best solutions below

3
On BEST ANSWER

Give this a go

DECLARE @Fromdate DATETIME = DATEADD(MONTH,-6,GETDATE())