Solved

DateTime Comparison in SQL and VB.NET

Posted on 2009-07-13
7
375 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 100 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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 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

Technology Partners: 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!

Question has a verified solution.

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

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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 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