by Admin
8. September 2009 17:06
I ran into an issue recently while comparing a datetime entry from an HTML form to a DateTime field in SQL. When .NET parses the entry into a DateTime object and the Time is omitted, it throws off the comparison when it gets to the SQL.
For instance, a user enters "9/8/2009" instead of "9/8/2009 5:00 PM"
To get around this, I came across this little hack for SQL which strips off the time from the SQL datetime when doing the comparison :
(SELECT CAST(FLOOR(CAST(someDate AS float)) AS datetime)
What this does:
1. Casts someDate to a float which truncates the time on conversion.
2. Casts back to a datetime which leaves you with a date like 9/8/2009 00:00:00
32cb69ba-ee4d-4e78-a563-4e1e5bc78bd5|0|.0
Tags: