Go Premium for a chance to win a PS4. Enter to Win

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
?
182 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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 month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

916 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