?
Solved

compare varchar in a query as a datetime

Posted on 2010-01-12
7
Medium Priority
?
401 Views
Last Modified: 2012-05-08
table was design a while ago as:

Tab
{
   id              uniqueidentifier not null,
   name        varchar(64),
   solddate        varchar(64),
   type          int
}

this query fails:
select count(*) as AssetsSold from Tab
where isdate(solddate) = 1
and solddate is not null
and convert(datetime,solddate,101)
between convert(datetime,'10/1/2009',101) and convert(datetime,'1/1/2010',101)

with the following error:
"Conversion failed when converting datetime from character string."


how can I pull these (varchar) values and compare them as dates?
0
Comment
Question by:ipaman
[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
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 26298086
whats the dateformat on solddate  
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 300 total points
ID: 26298819
try this, it checks solddate in that last where condition again, that is where you problem was.
select count(*) as AssetsSold from Tab
where isdate(solddate) = 1
and solddate is not null
and convert(datetime,CASE WHEN isdate(solddate) = 1 THEN solddate ELSE NULL END,101) 
between convert(datetime,'10/1/2009',101) and convert(datetime,'1/1/2010',101)

Open in new window

0
 
LVL 15

Expert Comment

by:MohammedU
ID: 26298902
Try the following...

select convert(char(10),solddate,101) as AssetsSold from AssetsSold
where isdate(solddate) = 1
and solddate is not null
and convert(char(10),solddate,101)
between convert(char(10),'10/1/2009',101) and convert(char(10),'1/1/2010',101)
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 32

Expert Comment

by:awking00
ID: 26303444
You apparently have data in the solddate field that is not in mm/dd/yyyy format. What does the following return?
select convert(datetime,solddate,101) from tab
0
 
LVL 32

Expert Comment

by:awking00
ID: 26303456
Is it possible you have null values in the solddate field?
0
 

Author Comment

by:ipaman
ID: 26304069
ok:
aneeshattingal: date format is not always known but typically mm/dd/yyyy

MohammedU: your returns no rows, even with different date values (that I know are valid)

awking00: yours returns many rows then errors when it htis a unusual format: Conversion failed when converting datetime from character string.
also, I am checking for nulls in my original query.

CGLuttrell: Yours returns a valid number. I suspect I am not getting all the dates because of the varying formats. probably nothing I can do about it. The query is probably the best in this circumstance.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 26304531
ipaman,
check whether your table contain invalid date values

select *
from tablename
where isdate(solidDate) = 0


Aneesh
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

771 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