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
156 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40399504
:: drum roll ::   :: symbol crash ::
0
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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