1

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

Dale K
  • 25,246
  • 15
  • 42
  • 71
JoSSte
  • 2,953
  • 6
  • 34
  • 54

1 Answers1

1

Give this a go

DECLARE @Fromdate DATETIME = DATEADD(MONTH,-6,GETDATE())
Matthew Baker
  • 2,637
  • 4
  • 24
  • 49
  • While this does solve the issue, the input field below the query dissapears, It's the `##` part of the declaration that does this. question is whether I can put your solution into that declaration – JoSSte Jan 23 '19 at 09:28
  • `DECLARE @Fromdate DATETIME = ##FromDate?DATEADD(MONTH,-4,GETDATE())##` works a charm – JoSSte Jan 23 '19 at 09:30
  • 1
    Yeah, what I've posted is meant to guide in the right direction. So long as its helped then I'm a happy DBA. – Matthew Baker Jan 23 '19 at 09:35