Replace Errors with NULL using Convert

From: Dan (Dan_at_discussions.microsoft.com)
Date: 02/01/05


Date: Tue, 1 Feb 2005 08:07:09 -0800

I have a table with a varchar field that for the most part contains valid
dates ie (mm/dd/yyyy). There are some items that are not dates. I would
like a SQL statement that converts the varchar to a datetime and where there
is an error for a particular field will return a null for that field.

For example if my table contains the following items in Field1:

2/1/2005
1/1/2004
other data
3/1/2005

The query should return:
2005-2-1 00:00:00
2004-1-1 00:00:00
NULL
2005-3-1 00:00:00

If I use:
SELECT convert(datetime, Field1) as Field1
FROM table1

Then I get a conver error. Any ideas or suggestions would be helpful.

Thanks!