Solved

DateTime Comparison in SQL and VB.NET

Posted on 2009-07-13
7
371 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

786 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