Re: Convert to DateTime

From: Andrew John (aj_at_DELETEmistrose.com)
Date: 03/01/04


Date: Mon, 1 Mar 2004 12:29:40 +1100

Elmo,

A straightforward way is to add a new datetime column to the table,
and run a succession of update queries from the old nvarchar to the new.

Easy to see when you have got them all by allowing NULLs in the new
column and doing selects to see where still null.

Query e.g.

Update MyTable
set NewDTCol = convert( datetime, substring( OldNVCCol, 21, 4) + '-10-' + substring( OldNVCCol, 9, 10) , 120 )
where substring( OldNVCCol, 5, 3 ) = 'Oct'
 and NewDtCol is NULL -- Not really required because of the 'Oct', but potentially prevents errors for the easier
non-matching formats that have already been converted

If you want to do all months in one hit, then you could use CASE.

Regards
 AJ

"Elmo Watson" <sputnik75043@nospam.Yahoo.com> wrote in message news:e0KB9gy$DHA.392@TK2MSFTNGP12.phx.gbl...
> Unfortunately, I've inherited a database with a field which has been adding
> dates and times, but it's been using an nvarchar field .
>
> Most of the strings are in this format:
> Mon Oct 13 12:58:00 2003 EST
>
> but of course, there are still strings that will convert to date and time
> more easily like:
> 4/11/2003 9:29:53 AM
>
> I've tried doing a left/right/update scenario to remove the Day and EST
> strings, but then, I'm left with strings like:
> Oct 13 12:58:00 2003
>
>
> I really would like to convert it to a date/time field, but even with that,
> apparently it doesn't like the Oct and gives me an error:
> - Unable to modify table.
> ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error
> converting datetime from character string.
>
> Anyone have any ideas on how I can do this in as few steps as possible?
>
>
>



Relevant Pages

  • Re: stored procedure question
    ... > I pass a stored procedure 2 strings: ... but it is looking for StringVal to be an INT ... > @StringVal1 nvarchar, ... > declare @NextString1 nvarchar ...
    (microsoft.public.sqlserver.programming)
  • Re: Anger, Rage, Confusion...
    ... > declared in the vb code as strings yet the parameters are declared as ... You can use an overload of the SqlParameter constructor that takes also the size ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Microsoft SQLServer - varchar or nvarchar fields?
    ... I'm tempted to use nvarchar (because jtds driver seems to send strings as ...
    (comp.lang.java.databases)
  • Re: JDBC - varchar or nvarchar fields?
    ... I'm tempted to use nvarchar (because jtds driver seems to send strings as ...
    (comp.databases.ms-sqlserver)
  • Re: Date Ranges
    ... strings in this way then they will be compared like strings. ... DATETIME column. ... makes it harder to specify ranges of whole days if your datetime values have ...
    (microsoft.public.sqlserver.programming)