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:

Comments

12/28/2009 7:53:58 AM #

Thank you for your help!

Michigan Payday Loans United States

1/16/2010 7:48:03 AM #

Putting off an easy thing makes it hard. Putting off a hard thing makes it impossible.

cash loans United States

3/1/2010 6:42:06 PM #

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.

buy guitar online United States

3/2/2010 1:14:12 PM #

Hey there,
I really love this weblog. Google sent me here. I have found so much <B>good imformation</B> here.
Will surely visit it again.

Hotels Phangan Germany

3/2/2010 9:46:59 PM #

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

downtown san diego real estate United States

3/10/2010 11:42:14 AM #

I have recently started using the blogengine.net and I having some problems here? in your blog you stated that we need to enable write permissions on the App_Data folder...unfortunately I don't understand how to enable it.

hp printer United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



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