Search code examples
sqlsql-server-2005datevarchar

SQL 2005 compare Date saved as VARCHAR


I have saved dates as VARCHAR in sql server 2005 now I want to select all dates before 2013 and I can't.

WDATE 09/01/2012 08/13/2012

I want to compare those dates with any other date I am using this query:

SELECT * FROM Work_Order
WHERE  WDATE  < '09/02/2012'

but it subtract days then months then years which is not my case


Solution

  • Seeing your updated question, you cannot do what you want (selecting records before some date) if your column is defined as VARCHAR. You need to store it as a DATETIME value or CAST it, perhaps like this:

    SELECT * 
    FROM   Work_Order 
    WHERE  CAST(WDATE as DATETIME)  < CAST('09/02/2012' as DATETIME)
    

    I don't have SQL SERVER 2005 myself, so the syntax is just a guess.