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
145 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
Comment Utility
No, it's because there is no June 31!
0
 
LVL 25

Accepted Solution

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

Expert Comment

by:Jim Horn
Comment Utility
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
Comment Utility
:: drum roll ::   :: symbol crash ::
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
$%@&%$#%! 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
Comment Utility
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
Comment Utility
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
Comment Utility
Shaun, you are missing an equals sign:

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

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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

10 Experts available now in Live!

Get 1:1 Help Now