T-SQL Operations on a Calculated Date Field
        Posted  
        
            by firedrawndagger
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by firedrawndagger
        
        
        
        Published on 2010-06-08T13:09:20Z
        Indexed on 
            2010/06/08
            13:12 UTC
        
        
        Read the original article
        Hit count: 254
        
Can I do WHERE operations on a calculated date field?
I have a lookup field, which has been written badly in SQL and unfortunately I can't change it. But basically it stores dates as characters such as "July-2010" or "June-2009" (along with other non date data). I want to extract the dates first (which I did using a LIKE opertor) and then extract data based on a date range.
 SELECT 
     BusinessUnit,
     Lookup,
     ReleaseDate 
 FROM 
 (
     SELECT TOP 10  
       LookupColumn As Lookup,
       BU as BusinessUnit,
       CONVERT(DATETIME, REPLACE(LookupColumn,'-',' ')) as ReleaseDate  
     FROM 
       [dbo].[LookupTable]
     WHERE 
       LookupColumn LIKE N'%-2010'
 ) MyTable
 ORDER BY ReleaseDate
 WHERE ReleaseDate = '2010-02-01'
I'm having issues with WHERE operator. I would assume creating a subquery to encapsulate the calculated field would allow me to do operations with it such as WHERE but maybe I'm wrong. Bottom line is it possible to do operations on calculated fields?
© Stack Overflow or respective owner