Question

I have a table with two columns - ID as int and date stored as nvarchar for some reason.

A non-clustered index was recently created on the ID field. Now in my query, when I convert the date from nvarchar to date, it gives me an 'Arithmetic overflow error converting expression to data type datetime.' exception.

If I remove the id field from my query, the conversion works perfectly. Similarly if I remove the date field conversion from my query, it again works perfectly. If I drop the index, the original query works perfectly.

My question now is, why is an index on an ID column messing up conversion in a what-I-think-as-unrelated, date column?

Any explanation on this is much appreciated. And Query is:

select e.date ,e.id as id from dbo.table1 e,dbo.table2 d with (nolock) 
where e.id=d.id and 
convert(datetime,e.date) between convert(datetime,'07/01/2012') 
and convert(datetime,'07/02/2012') group by e.date,e.id 
Était-ce utile?

La solution

The date column in your table has invalid date values. Adding indexes and changing what columns are returned changes the query plan for your query so it evaluates the invalid date values.

Autres conseils

etrast81

As i see your query i just suggest you to use the temp. table first convert your date nvarchar to datetime then join it with next table,

i thinks it work

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top