Link to home
Start Free TrialLog in
Avatar of Charlesdavid Allen
Charlesdavid AllenFlag for United States of America

asked on

Access Date/Time to SQL Datetime2(7)

2 tables:  

1 - MS Access (2015/O365):  
         Fields Loan (Text) and BatchDateTime (Date/Time)

2 - SQL Server 2016:
         Fields Loan (varchar(30)) and BatchDateTime (Datetime2(7))

Non passthrough query in Access:

       docmd.runsql "INSERT INTO dbo_tbl2 (Loan, BatchDateTime) " & _ 
                                 "SELECT tbl1.Loan, tbl1.BatchDateTime " & _
                                 "FROM tbl1;" 

Open in new window


Value of tbl1.batchdatetime = 1/25/2017 9:19:42 AM

The above SQL runs to no success for a type mismatch.  I tried adding format to the mix via:  FORMAT(tbl1.BatchDateTime, 'YYYY-MM-DD HH:NN:SS') to no success.  also tried modifying the tbl2.batchdatetime to datetime2(0) via ALTER COLUMN, data type changed but insert was still unsuccessful.  

I am stuck with Datetime2 as a datatype for the SQL due to vendor constraints.

I have attempted to search for an answer in EE but i find many going the other way, few going Access into SQL.

Need some guidance from the esteemed members of EE.
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charlesdavid Allen

ASKER

@Pat

    Downloaded and installed on test/dev machine.  Changed DNS from "DRIVER=SQL Server" to "DRIVER="ODBC Driver  for SQL Server",  relinked with new driver.  Attempted to run the above SQL with same failed result of a type conversion error.  Just as a note though, i am not using Datetime(2)  but instead Datetime2(7) that is datatype datetime2 and not datatype datetime to a specificity of 2.
Tried SELECT CONVERT(datetime2(7), tbl1.BatchDateTime ,120) with no success. Unknown Function CONVERT in expression.
Did you leave out the "11"?  The driver is "ODBC Driver 11 for SQL Server"

As it happens, we don't update the tables that I didn't create (mine are all DateTime because I am aware of the problems) but I don't have any trouble reading the DateTime(2) as a datetime field.  Using the old driver, that data type came over as a string so the dates wouldn't compare or sort as dates since they were strings.

I presume you relinked all the tables.
I did include the "11" i just mistyped.  C/p it reads DRIVER=ODBC Driver 11 for SQL Server;  Tables were relinked.  

I am not though trying to shove a Datetime(2) and read it as date/time,  (that would be SQL to Access, and both are technically datetime fields of similar length.

I am trying to shove a date/time into a Datetime2(7) (Access to SQL) and they are different formats.
For the sake of arguement, i changed BatchDateTime on tbl2 (SQL) side to datetime and still no dice.
NEVER format a date except for display.  Internally dates are stored as double precision numbers and when you format them, you turn them into strings and they no longer operate like a date.

When you link the SQL Server table, does the date look like mm/dd/yyyy or like mm-dd-yyyy?  If the latter, Access isn't seeing it as a date.  Try to sort the recordset and see if all the january entries for all years come before the february entries for all years.
On the SQL side there is no data in the table (until i append this data)

DoCmd.RunSQL "INSERT INTO tbl2 (BatchDateTime) " & _
                 "VALUES (now());"

Open in new window


works fine, and producers data in the (SQL tbl) tbl2.BatchDateTime field in the format 1/26/2017 2:04:46 PM (displayed in access)

the (Access table) tbl1.BatchDateTime field has data formatted as 1/25/2017 9:19:42 AM (displayed in access)
@Pat - Accepting solution of ODBC driver change.  

At some point last night/this morning i modified the tbl1.BatchDateTime field to text which caused the whole kerfuffle.