Friday, February 17, 2012

Date Compare

How do you compare date in sql, it is giving me errors,

I want to view all that logged onto my system Today
(*Yes I want to use dyn query for this *)

LastLogin is of type DateTime

SELECT @.myStatement = ' SELECT LastLogin, surname
FROM #TEMP WHERE ((LastLogin ='+CONVERT(datetime, @.LastLogin)+')

exec(@.myStatement)

Thanks in advanceLooks to me like you're trying to add a date type to a string, you can't do that. You need to make it a string.|||

SELECT LastLogin, surname
FROM #TEMP
WHERE LastLogin >= cast(cast(getdate() as char(12)) as datetime)
|||Ehorn where did the parameter go ?|||ehorns showing you how to cast the date conversion into a string. You need to apply that to your code, although its back to front for your example. Basically you need...

cast(@.LastLogin as varchar)

No comments:

Post a Comment