What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

Is it possible to have DATETIME parametrised queries on the Stack Exchange Data Explorer, e.g:

DECLARE @MyDate DATETIME = ##MyDate##
SELECT @MyDate

The output is usually completely unrelated to the input ('2010-01-01' converted to '1905-07-02') - date conversion functions don't seem to be much help either.

share|improve this question
    
SEDE doesn't actually parameterize queries -- it's merely a string replacement. Adding the :string hint auto-quotes when that process happens. –  Jon Seigel Jan 25 '11 at 18:20

3 Answers 3

up vote 2 down vote accepted

You should be using

DECLARE @MyDate DATETIME = ##MyDate:string##
SELECT @MyDate

AFAIK, SQL Server accepts strings for ANY datatype to be implicitly converted.

The snippet works on SEDE.

share|improve this answer

After posting this I also found that using "'2010-01-01'" (including the ' quotes) worked with the example posted in the question.

share|improve this answer

You can also use the following:

DECLARE @MyDate DATETIME
Declare @inputDate varchar(8)

Set @InputDate = ##MyDate##
set @MyDate = Convert(datetime, @InputDate)

SELECT *
From Users
Where CreationDate > @MyDate​​​​​​​
And CreationDate < DateAdd(day, 3, @MyDate)

http://data.stackexchange.com/programmers/s/840/using-dates

share|improve this answer
    
This didn't work for me - replacing the SELECT with SELECT @MyDate gave me 2008-01-01 when I entered "2010-01-01" –  Justin Jan 25 '11 at 8:59
1  
@Kragen - It needs to be in the format of 20080101 not 2008-01-01 –  Barry Jan 25 '11 at 9:02
    
Oh, that also works - a completely different format from what I was expecting though. –  Justin Jan 25 '11 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .