Log in
E-mail
Password
Show password
Remember
Forgot password ?
Become a member for free
Sign up
Sign up
New member
Sign up for FREE
New customer
Discover our services
Settings
Settings
Dynamic quotes 
OFFON

LEARNING TREE INTERNATIONAL, INC.

(LTRE)
SummaryQuotesChartsNewsCompany 
SummaryMost relevantAll NewsOther languagesPress ReleasesOfficial PublicationsSector news

Learning Tree International : Pasting Datetime Data into Excel Workbooks

10/12/2021 | 01:12pm EST
During a coffee break at a recent Learning Tree course, Excel Data Analysis Training, June C. commented that when she pasted results from the SQL Server Management Studio into Excel "weird things happened." Well, June, it's actually not weird, but I definitely know what you are talking about. Microsoft Excel and Microsoft SQL Server have datatypes that are similar, but definitely identical. When you import data into Excel from SQL Server, whether you are using ODBC or SQL Native Client, the underlying library of data import code generally does all the right things and the data seems fine. However, when you copy and paste, you are relying on Excel to correctly interpret the pasted data. There is no data access code behind the scenes to fix things up for you.Datetime data is particularly sensitive because both SQL Server and Excel must constantly convert back and forth between their internal datetime data and the familiar formatted dates that we humans are used to.Let's examine what happens when we paste data into an Excel workbook. To investigate fully, we will want to see how Excel interprets the pasted datatype. VBA has a function that will tell us the datatype of a variable; to use this function on a worksheet cell we will have to create a simple "wrapper" function. A completed version of the macro-enabled workbook is available here.Function VarTypeTest(v As Variant) As String
' a wrapper for VarType so it can be used from worksheets

Select Case VarType(v)
Case vbArray
    VarTypeTest = "vbArray"
Case vbBoolean
    VarTypeTest = "Boolean"
Case vbByte
    VarTypeTest = "Byte"
Case vbCurrency
    VarTypeTest = "Currency"
Case vbDataObject
    VarTypeTest = "DataObject"
Case vbDate
    VarTypeTest = "Date"
Case vbDecimal
    VarTypeTest = "Decimal"
Case vbDouble
    VarTypeTest = "Double"
Case vbEmpty
    VarTypeTest = "vbEmpty"
Case vbLong
    VarTypeTest = "Long"
'Case vbLongLong  '64-bit'VarTypeTest = "LongLong"Case vbNull
    VarTypeTest = "Null"
Case vbObject
    VarTypeTest = "Object"
Case vbSingle
    VarTypeTest = ""
Case vbString
    VarTypeTest = "String"
Case vbUserDefinedType
    VarTypeTest = "User Defined Type"
Case vbVariant
    VarTypeTest = "Variant"
End Select
End Function

This function will return a string describing how Excel has interpreted the datatype of the data pasted from the Management Studio.

Of course, we'll need some data to paste.

SELECTGETDATE()ASDatetime

,CAST(GETDATE()ASSMALLDATETIME)ASSmallDatetime

,CAST(GETDATE()ASDATETIME2)AS[Datetime2]

,SYSDATETIMEOFFSET()AS[DateTimeOffset]

,CONVERT(CHAR,GETDATE(),100)AS[Char Style 100]

,CONVERT(CHAR,GETDATE(),101)AS[Char Style 101]

,CONVERT(CHAR,GETDATE(),106)AS[Char Style 104]

,CONVERT(CHAR,GETDATE(),109)AS[Char Style 109]

Here is the resultset in the Management Studio:

[Link]

When we copy and paste, the results look like this:

[Link]

The Sql Server datetime and datetime2 datatypes look odd, but are actually OK. Excel correctly interprets the data as datetime data, but applies a custom format.

[Link]

If we manually change the formatting to a date format of our liking, we see that the format changes for some, but not all of the "dates". Similarly, if we attempt to add a day to each of the "dates" we fail for some. To appreciate the reason for this, we apply the VarTypeTest macro function to the data imported from Excel. We observe that some SQL Server output datatypes and formats are interpreted as dates, two are stored as doubles in Excel, but some SQL Server datatypes (shown in red) are not recognized as dates at all and are stored in Excel as string data. These cells will not accept a different datetime format and cannot be used as dates in calculations.

[Link]

Note that formatted outputs from the CONVERT function in SQL are most definitely character data when they leave SQL Some formats can be correctly interpreted by Excel as dates, others not.

One Last Peculiarity

It seems so obvious that all the dates in this example should be the same, since they all (save one) originate by calling the GETDATE( ) function. But in fact, something interesting is going on behind the scenes. Although the SQL Server datetime and smalldatetime datatypes and the Excel date datatype store a date as the number of days since January 1, 1900, they do not do this calculation in that same way. Therefore, even though Excel seems to be treating the datetime data as a double datatype, the actual numerical value must have been adjusted during the paste operation to have the Excel value, not the SQL Server value. Let's try one more experiment.

We will get today's date from SQL Server using the Management Studio, and we will also convert this date to a float datatype, the equivalent of Excel's double datatype.

SELECTGETDATE()ASDatetime

,CAST(GETDATE()ASFLOAT)ASFloat

[Link]

When we paste this data into Excel, we get a surprise:

[Link]

The values for the same date are off by two! Part of this inconsistency is easy to explain. SQL Server treats January 1, 1900 as day 0, while Excel treats it as day 1. That explains a difference of one, but not two.

The reason for the second day of difference is far, far odder. Let's use the Excel date format tools to look at the day of the week for February 29, 1900.

[Link]

Looks like February 29, 1900 was a Wednesday. On small problem, though. There was no February 29, 1900. 1900 was not a leap year. When Excel was first introduced many years ago, Lotus 1-2-3 dominated the spreadsheet market. The developers of Lotus took a shortcut in date calculation, and for most applications erroneously treating 1900 as a leap year made no difference. Microsoft, just entering the market, decided that compatibility with Lotus was critically important for success in the marketplace, and the Excel developers intentionally included this error in their code.

Conclusion

I have always been a fan of the ease of copy-and-paste to move small datasets from the Management Studio into Excel. But when the datasets include datetime data, care must be taken. It is often helpful to use the CAST function in SQL statements to convert to the SMALLDATETIME datatype, which is correctly recognized and formatted by Excel. If you use CONVERT to output a character representation of a date, make certain that Excel recognizes the format as a date and not as a string.

Disclaimer

Learning Tree International Inc. published this content on 12 October 2021 and is solely responsible for the information contained therein. Distributed by Public, unedited and unaltered, on 12 October 2021 17:11:01 UTC.


ę Publicnow 2021
All news about LEARNING TREE INTERNATIONAL, INC.
01/20LEARNING TREE INTERNATIONAL : Manage Yourself and Manage Your Team with Office 365
PU
01/11Duke Corporate Education and Learning Tree Form New Partnership to Cultivate Key Leader..
CI
01/11LEARNING TREE INTERNATIONAL : Expand the Pipeline of Job-Ready Cloud Risk Management Profe..
PU
01/11LEARNING TREE INTERNATIONAL : Duke Corporate Education and Learning Tree Form New Partners..
PU
2021LEARNING TREE INTERNATIONAL : Top 10 CMMC 2.0 Questions and Answers
PU
2021The new Cybersecurity Maturity Model Certification (CMMC)
PU
2021LEARNING TREE INTERNATIONAL : How to Use PortableApps to Make Your Life Easier
PU
2021SOME QUERY HINTS : Undocumented, But Interesting
PU
2021LEARNING TREE INTERNATIONAL : How deMorgan's Theorems Can Help Programmers
PU
2021LEARNING TREE INTERNATIONAL : Pasting Datetime Data into Excel Workbooks
PU
More news
Chart LEARNING TREE INTERNATIONAL, INC.
Duration : Period :
Learning Tree International, Inc. Technical Analysis Chart | LTRE | US5220151063 | MarketScreener
Managers and Directors
David Brown Chief Executive Officer & Director
Igor Lima Chief Financial Officer
Kevin Ross Gruneich Non-Executive Chairman
Magnus Nylund Chief Operating Officer
Richard J. Surratt Independent Director