[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DateTime Comparison in SQL and VB.NET

Posted on 2009-07-13
7
Medium Priority
?
378 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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 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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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