Solved

DateTime Comparison in SQL and VB.NET

Posted on 2009-07-13
7
369 Views
Last Modified: 2013-11-27
Hi,

I am trying to compare two dates which I believe are in long datetime format although I receive the following error -

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

Code in VB -

Select * FROM Project WHERE (ProjectDate <= '" & Trim(DateTimePicker1.Value) & "')"
0
Comment
Question by:glit
7 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 100 total points
ID: 24841058
Pass the date in odbc format YYYY-MM-DD or YYYYMMDD
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24841092
What datetime format do you get when you do this in your VB application (see code snippet)?
If the format is not what your SQL server is expecting, then you may get the issue you are seeing.  For example if your application is generating dd/MM/yyyy and SQL is expecting MM/dd/yyyy then a date such as this would be invalid: 13/07/2009.
MsgBox(DateTimePicker1.Value)

Open in new window

0
 

Author Comment

by:glit
ID: 24841097
sorry, how do I do that?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24841107
why don't you try something like this?

Select * FROM Project WHERE (ProjectDate <= convert(varchar(20),'" & Trim(DateTimePicker1.Value) & "',120)
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 400 total points
ID: 24841128
Exactly, as Aneesh indicated.  I was going to suggest you converting the format accordingly, but Aneesh has already posted a good suggestion regarding that.  To implement, just use the ToString of date and pass in the format string corresponding to the ODBC format shown.
Select * FROM Project WHERE (ProjectDate <= '" & DateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss") & "')"

Open in new window

0
 

Author Comment

by:glit
ID: 24841258
Thanks guys, that worked!
0
 

Author Comment

by:glit
ID: 24841259
Thanks guys, that worked!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now