I help out a collegue with exporting data from SQL Server 2012. I type manual queries and then copy/paste the results to an Excel sheet that I then share with him. The extract I make will be an excel file that will be manually analysed. No need for automation.
What I try to do is settling for best practice when exporting order statistics grouped by day, trying to learn SQL better. I have a field of type datetime
, and I want to convert this to some date format. In my locale, the typical date format is YYYY-MM-DD
. I do sometimes share my script with others, that might have another locale. I have found three statements that seem to yield the same values for me.
select top 1
createdat
, CAST(createdat as date) as A
, CONVERT(char(10), createdat,126) as B
, CONVERT(char(10), createdat,127) as C
from dbo.[Order]
resulting in
createdat |A |B |C
2012-12-27 08:23:32.397 |2012-12-27 |2012-12-27 |2012-12-27
From the TSQL MSDN reference (link) I understand that:
- A is handled by SQL as type Date, whereas B and C are chars.
- B and C should differ by their time zone handling.
But I dont understand:
- HOW does B and C handle time zones?
- What is the practical difference when copy/pasting to Excel?
- Is there practical difference if I share this script with collegues using another locale I should consider?
- Should one or the other be preferred?