Re: Error converting a text field to a datetime datatype, who can figure this anyone????

From: Erland Sommarskog (sommar_at_algonet.se)
Date: 03/02/04


Date: Tue, 2 Mar 2004 23:17:16 +0000 (UTC)

Sd (nowhere@sadfs.com) writes:
> I have been trying to convert whatever I can but I keep getting errors
> when I perform a
>
> SELECT CONVERT(datetime, text_field) FROM Old_data
>
> Obviously my select statemt is abruptly terminated when I hit one of these
> records with a invalid date.
>
> I tried looping throught the records using a curson and try to capture and
> check for errors:

But since SQL Server thinks conversion error is reason enough for aborting
the batch, that does not fly.

But this should do:

   SELECT CASE WHEN is_date(text_field) = 1
               THEN convert(datetime, text_field)
          END
 

-- 
Erland Sommarskog, SQL Server MVP, sommar@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp


Relevant Pages

  • Re: Insert Error: Column name or number of supplied values does not match table definition.
    ... submitting the batch, ... You need to learn about parameterised statements. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (microsoft.public.data.oledb)
  • Re: long SQL lines for osql
    ... it sounds like it chokes on something. ... It could be the file size, but it could also be the batch size. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: long SQL lines for osql
    ... it sounds like it chokes on something. ... It could be the file size, but it could also be the batch size. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: run Profiler on a specific SP/Query/Function
    ... events help you get a better grip over the flow? ... Do Starting events let you know if a batch is aborted due to an error any ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (microsoft.public.sqlserver.tools)
  • Re: Query training -- Complex queries
    ... > Update PositionsEOM ... Here you don't have a derived table, but a correlated subquery. ... UPDATE in Books Online. ... is not written for SQL Server. ...
    (microsoft.public.sqlserver.server)