Question : DateTime and Varchar

in the following statement

SELECT Employee.[SSO], EmployeeDetail.[MVCAStartDate] FROM Employee, EmployeeDetail WHERE EmployeeDetail.[MVCAStartDate] <= CAST('02/04/2008' as DateTime) AND Employee.[ID] = EmployeeDetail.[EmployeeID] ORDER BY CAST(EmployeeDetail.[MVCAStartDate] as DateTime)

It returns the error
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value

The reason is that throughout the value '00/00/0000' has been inserted many times. I cannot change the source data I have been told. How can I alter this statement to handle this problem? I think if I can tell the sql statement to convert these to nulls it won't bother it. But I am not sure.

Answer : DateTime and Varchar

Hi Have your tried the fix for the THEN Error
Cheers, Andrew
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
SELECT Employee.[SSO]
     , CASE WHEN ISDATE(EmployeeDetail.[MVCAStartDate])=1 
            THEN CAST(EmployeeDetail.[MVCAStartDate] AS datetime)
            ELSE NULL
            END  AS dt1
 
FROM Employee, EmployeeDetail 
 
WHERE Employee.[ID] = EmployeeDetail.[EmployeeID] 
AND   CASE WHEN ISDATE(EmployeeDetail.[MVCAStartDate])=1 
           THEN CAST(EmployeeDetail.[MVCAStartDate] AS datetime)
           ELSE NULL
           END  <= CAST('02/04/2008' as DateTime) 
 
ORDER BY CASE WHEN ISDATE(EmployeeDetail.[MVCAStartDate])=1 
              THEN CAST(EmployeeDetail.[MVCAStartDate] AS datetime)
              ELSE NULL
              END
Open in New Window Select All
Random Solutions  
 
programming4us programming4us