Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
171 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
[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
  • 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 27

Accepted Solution

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

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 66

Expert Comment

by:Jim Horn
ID: 40399504
:: drum roll ::   :: symbol crash ::
0
 
LVL 27

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 66

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

722 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