DateTime fields from SQL Server display incorrectly in Excel

后端 未结 9 609
独厮守ぢ
独厮守ぢ 2020-12-12 16:34

Countless times during the day I am copying and pasting records from SQL Server Management Studio to Excel.

My problem is that a DateTime value such as

9条回答
  •  萌比男神i
    2020-12-12 16:54

    Here's a hack which might be helpful... it puts an apostrophe in front of the time value, so when you right-click on the output in SSMS and say "Copy with Headers", then paste into Excel, it preserves the milliseconds / nanoseconds for datetime2 values. It's a bit ugly that it puts the apostrophe there, but it's better than the frustration of dealing with Excel doing unwanted rounding on the time value. The date is a UK format but you can look at the CONVERT function page in MSDN.

    SELECT CONVERT(VARCHAR(23), sm.MilestoneDate, 103) AS MilestoneDate, '''' + CONVERT(VARCHAR(23), sm.MilestoneDate, 114) AS MilestoneTime FROM SomeTable sm

提交回复
热议问题