[Solved] Unable to Subtract Dates SQL Server


I see from your post edit last time, value from field datetime like ’20/Mar/2013:03:17:44′. Format from value of datetime can’t convert by a simple way.

Try this query:

SELECT BASESCORE, 
       [DATEDIFF]= Datediff(second, Min(CONVERT(DATETIME, 
                                        LEFT(Rtrim(Ltrim(DATETIME)), 
                                        11) + ' ' 
                                        + RIGHT(Rtrim(Ltrim(DATETIME) 
                                        ), 8))), Max 
                               ( 
                               CONVERT(DATETIME, LEFT(Rtrim(Ltrim(DATETIME)), 11 
                                                 ) + ' ' 
                                                 + RIGHT(Rtrim(Ltrim(DATETIME)), 
                                                 8)))) 
FROM   temptabel 
GROUP  BY BASESCORE 
ORDER  BY BASESCORE 

Output:

Basescore    datediff
1            1
2            0
3            0

You can see at SQLFiddle

3

solved Unable to Subtract Dates SQL Server