Re: Replace Errors with NULL using Convert

From: Steve Kass (skass_at_drew.edu)
Date: 02/01/05


Date: Tue, 01 Feb 2005 11:39:12 -0500
To: Dan <Dan@discussions.microsoft.com>

Dan,

  See if this works:

select
  case when ISDATE(Field1) = 1
       then cast(Field1 as datetime)
  else NULL end
from table1

If you can't live with the fact that some garbled
data may convert unexpectedly (the string '110919'
will convert to September 19, 2011, for example),
you'll have to do some pattern matching of your own
as well, such as

case when Field1 like '%/%/%' and Field1 not like '%/%/%/%' ...
and isdate(Field1) = 1 then ...

Steve Kass
Drew University

Dan wrote:

>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!
>
>
>
>



Relevant Pages

  • Re: Help to identify different strings
    ... I advise against importing the log file into a "Table1" with one record ... Dim strLine As String ... I agree - it is easy to parse each individual line in Table1 by applying ... individual alarm type, then check whether this string is available in the ...
    (microsoft.public.access.modulesdaovba)
  • Re: Help to identify different strings
    ... Dim strLine As String ... 'XXX is the name of the field in Table2 that contains ... I agree - it is easy to parse each individual line in Table1 by applying ... individual alarm type, then check whether this string is available in the ...
    (microsoft.public.access.modulesdaovba)
  • Re: NULL / NOT NULL Query
    ... Sammlung von Strings in Table1 eine Untergruppe ... Table2 abgelegt sind - nach folgendem Muster: ... INSERT INTO Table1 (string) values ...
    (microsoft.public.de.sqlserver)
  • Re: Help to identify different strings
    ... I agree - it is easy to parse each individual line in Table1 by applying ... different queries checking whether a significant string is available or not. ... be maintained in a table (Table2). ... individual alarm type, then check whether this string is available in the ...
    (microsoft.public.access.modulesdaovba)
  • NULL / NOT NULL Query
    ... Sammlung von Strings in Table1 eine Untergruppe ... Table2 abgelegt sind - nach folgendem Muster: ... INSERT INTO Table1 (string) values ...
    (microsoft.public.de.sqlserver)