I'm doing a MSSQL to MySQL data migration. My date/time value is exactly how it needs to be in both databases. However, VBScript converts this (by default):
2016-01-06 10:26:30.363
To this (which errors on INSERT
):
1/6/2016 10:26:30 AM
I'm aware I can construct the value again with Year(), Month(), etc. Anyone know how to get this date/time value unaltered from the database using VBScript?
UPDATE: Per the "show my code" comment, here's a section:
strSQL = "SELECT * FROM Users"
Set objRS = CreateObject("ADODB.RecordSet")
objRS.Open strSQL, objConn,3,3
Do While Not objRS.EOF
sUserId = objRS("UserId")
sCreatedDate = objRS("CreatedDate") '<-- At this point, it's "converted" already
wscript.echo sCreatedDate '<-- This displays 1/6/2016 10:26:30 AM format
insertSQL = "INSERT INTO northwind.usersetting (UserId,CreatedDate) "
insertSQL = insertSQL & "VALUES ('"&sUserId&"','"&sCreatedDate &"');"
objConn2.Execute = (insertSQL)
objRS.MoveNext
Loop
Just your standard vbscript. I think I can get away with this in MSSQL, but MySQL doesn't like it. In the meantime, I have done this to work around the issue:
Function FormatDate4Insert(date)
FormatDate4Insert = Year(date) & "-" & Month(date) & "-" & Day(date) & " " & Hour(date) & ":" & Minute(date) & ":" & Second(date)
End Function
I would love to do a straight non-conversion, but vbs seems to convert no matter what I do. I tried converting to string and a few other things with no joy...
P.S. I'm not sure what you mean by database import export mechanisms. However, I tried the MySQL migration tool as well as exporting and import mechanisms (to csv...with different delimiters and such... and even a json export, massaging the data with Notepad++ and Excel, etc) and can't get the data to jive with my selective import. I can migrate an entire database without issue for the most part, but simply want to do the data from an individual table. When I kill too much time, I usually just fall back to vbscript or whatever scripting makes sense.
I find both databases more forgiving and finicky in some areas. However, with MSSQL to MySQL, I have to convert empty values to "NULL" and True/False to their bit values (e.g. b'0') and other little tweaks that scripting makes easier (at least for me).
UPDATE 2: The error as requested:
Microsoft OLE DB Provider for ODBC Drivers: [MySQL][ODBC 8.0(w) Driver][mysqld-8.0.16]Incorrect datetime value: '1/6/2016 10:26:30 AM' for column 'CreatedDate' at row 1.
I can't reproduce the other error, but I was also getting an error that was something similar to this:
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1/6/2016 10:26:30 AM'
That was in reference to the Insert statement.