Comparing SQL Datetime with input Datetime parameters

by Admin 8. September 2009 17:06
Bookmark and Share

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

Tags:

Powered by BlogEngine.NET 1.5.0.7
Theme by Marcell Scarlett


My name is Marcell. I am 21 years old and I work full time as a Web Developer. I love computers, cars and the Web.

Social Networks

  • facebook
  • myspace
  • twitter
  • linkedin
  • youtube


Category list

None

Tag cloud

    RecentPosts

    Page List