Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
AM getting this error while performing this query operation

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

here' s my query

SQL
SELECT 
 MaxDate,
 MinDate,
DATEDIFF(D, MaxDate, MinDate)
FROM (
SELECT
     (SELECT MAX(TDate) ) AS MaxDate
    ,(SELECT MIN(TDate) ) AS MinDate
FROM EDATA
)a
Posted
Updated 28-Aug-14 4:05am
v2
Comments
[no name] 28-Aug-14 9:46am    
Store date/time as datetimes not as strings.
Abhishek Jaiswall 28-Aug-14 9:55am    
I didn't get you!
PhilLenoir 28-Aug-14 9:47am    
What is your data type of TDate and do you have an example of a value causing the error?
Abhishek Jaiswall 28-Aug-14 9:54am    
My datatype is date and dates are stored like this '3/3/12' ..
PhilLenoir 28-Aug-14 10:02am    
Where is the query being run? The stucture of your query is a little odd, but it works (although you will get a negative number from your DATEDIFF!) Is EDATA a table or is it a SQL View? Somehow your query processor is treating TDATE as a string.

try this

SQL
SELECT MAX(TDate)  AS MaxDate, MIN(TDate) AS MinDate , 
Datediff(d, MAX(TDate), Min(TDate)) FROM EDATA


or

SQL
SELECT
MaxDate,
MinDate,
DATEDIFF(D, MaxDate, MinDate)
FROM (
SELECT MAX(TDate)  AS MaxDate, MIN(TDate) AS MinDate FROM EDATA
)a
 
Share this answer
 
Comments
Abhishek Jaiswall 29-Aug-14 0:02am    
Still getting same error through this but, no worries. I have solved it my own. Thnks for your kind reply. #CHeers!
What i did is-
1: Changed my table creation query and used DATETIME in place of DATE

CREATE TABLE EDATA
(
ID INT not null,
Name NVARCHAR(50)not null,
TDate DATETIME not null,
TCard INT not null
);

2:
and then simply query is working without using CAST function, as
SQL
SELECT
MaxDate,
MinDate,
DATEDIFF(D, MaxDate, MinDate)
FROM (
SELECT
   MAX(TDate) AS MaxDate,
   MIN(TDate) AS MinDate
   FROM EDATA
   GROUP BY TCard
)a
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900