?
Solved

DateTime Comparison in SQL and VB.NET

Posted on 2009-07-13
7
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 75

Assisted Solution

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

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 60

Accepted Solution

by:
Kevin Cross earned 1600 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

801 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