views:

50

answers:

5

Hi All,

I am writing a query to fetch results for all the values in a column which is of varchar type.. are less than '29/08/2010' (date)

My Query:

SELECT * FROM EMPLOYEES WHERE COLUMN1 < '29/08/2010'

Here the column1 is of varchar type. I am using SQL Server 2005

Is there a way to make this possible..??

Pls help me.

Thanks in advance

+1  A: 
Convert(field,datetime,101)

http://www.sqlusa.com/bestpractices/datetimeconversion/

simply denis
+2  A: 

you can try

SELECT * FROM EMPLOYEES WHERE convert(datetime,COLUMN1,110) <  convert(datetime, '29/08/2010',110)
anishmarokey
I got this error... The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value
kayteen
in column there will be some invalid date will be there
anishmarokey
I checked the data... and i see that all the values are of this format 'dd/mm/yyyy'
kayteen
mine and @ramad answer is same rt? why it is not working.
anishmarokey
+1  A: 

Have you tried

SELECT * FROM EMPLOYEES WHERE CONVERT(datetime, COLUMN1, 103) > CONVERT(datetime, '29/08/2010', 103)
ramad
Ahh yes - changed it now, and see that it is the same answer as above - Doh :)
ramad
Except that I see that the above example uses 110 (mm-dd-yy) - I would say that it should be 103 (dd/mm/yyyy).
ramad
where is the where and not whhere
anishmarokey
worked like a charm.. thanks to you and anish...!!
kayteen
A: 

If your strictly storing Data data on your COLUMN1, then why is your defined your data type as varchar?

From the performance point of view, It will kill the performance, because Converting Varchar to DateTime or any other will make your indexes inefficient if there is any. Generally using functions like RIGHT() or CONVERT, LIKE'% %' or Like 'a%' will make the WHERE Clause a Non-Sargable, resulting an index scan operation to retrieve the data.

Cyborg
i am having this column as a dynamic one.. for generating dynamic sql... sometimes i may get values like NAME/EMAIL/CURRENCY/DATE
kayteen