Home All Groups Group Topic Archive Search About

Trouble using sql IN statement with list of datetimes

Author
30 Dec 2005 3:25 AM
JavaJeff@gmail.com
I would like to pass in a list of dateTime stamps in a nVarChar
parameter like so:

--Example input data:
'2005-12-21 15:55:26.053, 2005-12-21 15:41:35.600, 2005-12-21
15:23:21.647, 2005-12-20 19:15:27.180'

declare @RequestCreationTimeList nvarchar(500)

Select dbo.MyTable.RequestCreationTime, FieldA, FieldB
where (  convert(varchar, dbo.MyTable.RequestCreationTime,21)
IN (' + @RequestCreationTimeList + ') )


I am not able to get any matches...
I have a feeling it has to do with not having quotes in between the
individual datatime stamp.

This works:

   select dbo.MyTable.RequestCreationTime, convert(varChar,dbo.
MyTable.RequestCreationTime,121) as [String]

from dbo.MyTable

where (convert(varchar,dbo.MyTable.RequestCreationTime,21) =
'2005-12-21 15:55:26.053')

-- output:
    2005-12-21 15:55:26.053    2005-12-21 15:55:26.053

So I'm pretty sure my formatting is correct...
If any one has any insight on this I would be most appreciative.

Thanks,

Jeff

Author
30 Dec 2005 10:29 AM
ML
You can't pass a delimited string inside an IN expression without preparing
the query string (i.e. without using dynamic SQL).

Try this function by Dejan Sarka:
http://solidqualitylearning.com/blogs/dejan/archive/2004/10/22/200.aspx


ML

---
http://milambda.blogspot.com/
Author
30 Dec 2005 3:52 PM
JavaJeff@gmail.com
Many thanks for the lightning fast response! =)
Author
3 Jan 2006 9:21 AM
ML
Don't forget to thank Dejan in his blog. :)


ML

---
http://milambda.blogspot.com/

AddThis Social Bookmark Button