Solved

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

Posted on 2014-10-23
9
150 Views
Last Modified: 2014-10-23
I have this query that throws the error message in the title:

select top 100 * from custinvoicejour 
where INVOICEDATE between  '4/1/2014' and '6/31/2014'

Open in new window


When I run this query, it works.

select top 100 * from custinvoicejour 
where INVOICEDATE >  '4/1/2014'

Open in new window


It doesn't like the between statement! All dates in the database are valid and the field is a datetime field. I've tried cast and convert within the data parameters and no dice. I can't make sense of this one. Any suggestions?
0
Comment
Question by:mossmis
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40399495
No, it's because there is no June 31!
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40399498
6/31/2014 is not a valid date.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40399503
There is no such date as 6/31/2014, unless we're talking some alternate reality where there's also a February 30th.

SQL can't convert ''6/31/2014' to a date, so it considers it a string, hence the error.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40399504
:: drum roll ::   :: symbol crash ::
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40399506
You should also use the <my datefield> is greater than <start date> and <my datefield > is less than <end date +1>

So:
where INVOICEDATE >  '4/1/2014' and INVOICEDATE < '7/1/2014'
0
 

Author Closing Comment

by:mossmis
ID: 40399510
$%@&%$#%! Not a good day for me today. I'm going to have to talk to the account person who gave me that range....
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40399511
You have

where INVOICEDATE between  '4/1/2014' and '6/31/2014'

If you change it to

where INVOICEDATE between  '4/1/2014' and '6/30/2014'

It should work.

However, there are two dangers here.

All of this assumes that the end user or whoever is writing the SQL is using American-style dates, which may a dangerous assumption. You may find it better using:

where INVOICEDATE between  '20140401' and '20140630'

Open in new window


However, what type is INVOICEDATE? In your title to this question, you said "The conversion of a varchar data type to a datetime data type". If INVOICEDATE is a datetime data type, this will not capture 30 June 2014 at 1 a.m.

Therefore, this is better:

where INVOICEDATE>='20140401' and INVOICEDATE<'20140701'

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40399514
Look at the bright side ... It's far better to play the 'I'm an idiot' card with us then with multiple coworkers.

>I'm going to have to talk to the account person who gave me that range....
Make sure you act real smart like you knew it all along.  It'll be our little secret..
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40399545
Shaun, you are missing an equals sign:

where INVOICEDATE >=  '4/1/2014' and INVOICEDATE < '7/1/2014'

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
set default date format in mssql to mm/dd/yyyy 22 68
SQL Server Deadlocks 12 50
recover sqlserver db 8 55
ms sql last 8 weeks as columns 5 28
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

920 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

14 Experts available now in Live!

Get 1:1 Help Now