how to convert a MySQL date (linked server) to MS SQL?

  • Hi, I'm stuck on converting a MySQL date field (on a linked server) to MS SQL datetime format. I've googled this, but am having trouble finding something useful (other than using OpenQuery, but that's not ideal for a large update command with joins between the MySQL and MS SQL tables).

    Here's an abbreviated version of my update command:

    UPDATE dbo.NCU_Apply_FromWeb

    SET guardian1_attended_date = A_D.guardian1_attended_date

    from NCU_Apply_FromWeb NAW

    inner join mysql_apply...apply_2 A_D on A_D.uid = NAW.uid

    Error message:

    Msg 8114, Level 16, State 8, Line 1

    Error converting data type DBTYPE_DBTIMESTAMP to datetime.

    Any suggestions?

  • At a guess, you're encountering out-of-range values from MySQL (probably lovely old '0000-00-00 00:00:00') - if you can convert these to null it should work, I think. Last time I did this it was using OPENQUERY so not sure if it'll work in a straight join.

    Cheers

    Gaz

  • Thanks, Gaz, but that's not it.

    The MySQL values are:

    2013-02-03 00:00:00.000

    2013-02-11 00:00:00.000

  • Hi,

    Are those values for the entire table, not just the matching rows? My feeling is SQL will grab all the data before trying the join.

    Thanks

  • Those are the values for A_D.guardian1_attended_date (from the query above). Yes, there are only 2 rows that are joined.

  • Thanks, does select * from mysql_apply...apply_2 return the data sucessfully?

  • No. I neglected to mention I use this before the select:

    DBCC TRACEON(8765)

    However, I still get

    Msg 8114, Level 16, State 8, Line 2

    Error converting data type DBTYPE_DBTIMESTAMP to datetime.

    when I run

    DBCC TRACEON(8765)

    select * from mysql_apply...apply_2

  • No worries, not a trace flag I've used before but not sure it'll help with date fields.

    Out of range datetimes was about all I had for this - hopefully someone else will be along with another suggestion.

    Cheers

    Gaz

  • Thanks anyways, Gaz!

  • No worries. Just noticed these are your first posts on the site - welcome to SSC!

  • Hello! A wee bit of history, MySQL sometimes is used on a basis of "grab that web data, if they forgot the time do it anyhow!".

    So, you may have to assign a special date/time that will not come up in normal use, like jan, 01 am, 01 mins after the how, 01 secs after the min, and keep on til you fill up all the fields.

    Ugly as a mud fence, but that is one way.. now, if all of the times are important, I'll look around for something, this is first thing in the morning, and i'm not too awake yet! 😉

    hope it helps.. btw, I'm both a MySQL and SQL Server dba..

    J.

  • you could use DATE_FORMAT() to deal with this issue

    SELECT T.field

    FROM

    OPENQUERY(LINKSERVER, 'SELECT DATE_FORMAT(t.field,''%X-%m-%d %H:%i:%f'') as field FROM table t ' ) AS T

    the date '0000-00-00 00:00:00' will be replaced by ==> 00-00-0001 00:00:00

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply