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
Although not a complete answer to your question, there are shortcut keys in Excel to change the formatting of the selected cell(s) to either Date or Time (unfortunately, I haven't found one for Date+Time).
So, if you're just looking for dates, you can perform the following:
Ctrl
+Shift
+3
For formatting as Times, use Ctrl
+Shift
+2
.
You can use this in SQL SERVER
SELECT CONVERT(nvarchar(19),ColumnName,121) AS [Changed On] FROM Table
This is a very old post, but I recently encountered the problem and for me the following solved the issue by formatting the SQL as follows,
SELECT CONVERT (varchar, getdate(), 120) AS Date
If you copy the result from SQL Server and paste in Excel then Excel holds the proper formatting.
Try the following: Paste "2004-06-01 00:00:00.000" into Excel.
Now try paste "2004-06-01 00:00:00" into Excel.
Excel doesn't seem to be able to handle milliseconds when pasting...
I also had an issue with this problem simply copy and pasting DATETIME fields from SQL Management Studio to Excel for manipulation. Excel has the correct value of the DATETIME (even if the formatting is applied after the paste), but by default doesn't have a built in format to show the SQL DATETIME. Here's the fix:
Right click the cell, and choose Format Cells. Choose Custom. In the Type: input field enter
yyyy-mm-dd hh:mm:ss.000
Reference: http://office.microsoft.com/en-us/excel-help/create-a-custom-number-format-HP010342372.aspx
I had the same problem as Andre. There does not seem to be a direct solution, but a CAST in the query that generates the data fixes the problem as long as you can live within the restrictions of SMALLDATETIME. In the following query, the first column does not format correctly in Excel, the second does.
SELECT GETDATE(), CAST(GETDATE() AS SMALLDATETIME)
Perhaps the fractional part of the seconds in DATETIME and DATETIME2 confuses Excel.
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