Home » excel » MS SQL Server Dates Excel

MS SQL Server Dates Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have data this is linked from SQL Server into an excel document. The column format on the SQL Server is datetime2.

When I get the data via an ODBC connection it comes across as a string?

I tried using

CAST(column AS DATE ) 

but that didn’t work.

I tried reformatting via

CONVERT(VARCHAR(10), column, 103) 

as well but that didn’t work.

I tried retrieving the data via Microsoft query as well but that didn’t work.

At the moment I am using VBA code like:

While (ActiveCell.Value <> "")
  ActiveCell.Value = DATEVALUE(ActiveCell.Value)
  ActiveCell.Offset(1,0).Activate
Wend

and looping through each column that needs this treatment but 100000 rows in multiple columns takes forever to loop through. Are there any alternatives?

How to&Answers:

It appears that Excel and Access as well don’t like the SQL Server datetime2 format.

I have converted the data into datetime format and that seems to be working now.

Answer:

You can simply convert date in the Excel to the following format:

“YYYY-MM-DD HH:mm:ss”

This is the default ODBC canonical format. In this case the implicit conversion will be made and the correct results will be achieved.

Another way is to try to perform convert using another style:

CONVERT(VARCHAR(10), column, 120)

Answer:

No MACRO’s required, us Excel table columns instead.

Any ODBC connections in Excel returns a Table.
Simply add a new column to the right of your table with a calculation.
It should look something like this
=DATEVALUE([@DateColumnFromODBC])

The column will add and shrink with your table and always retain it’s reference as long as the ODBC connection stays the same.