DateTime fields from SQL Server display incorrectly in Excel - excel

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 8/23/2013 4:51:02 PM does not display correctly as shown in the image below even though it shows correctly in the function box.
Changing the datatype to ShortDate fixes the display issues, but it is tedious to do since I have lots of date fields to format. Since Excel and SQL Server are both Microsoft products one would expect that Excel would be able to correctly show the date field.
EDIT: So this appears to be a display issue with Excel. I copied my Management Studio results to Notepad and from there into Excel and I still get the same issue.
Copying 2013-08-23 16:52:11.493 from Notepad to Excel will display 52:11.5 (displaying minutes, seconds and rounded milliseconds).
Excel version is 2010.
Any ideas to simplify this?

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.

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.

Found a solution that doesnt requires to remember and retype the custom datetime format yyyy-mm-dd hh:mm:ss.000
On a new cell, write either =NOW() or any valid date+time like 5/30/2017 17:35: It will display correctly in your language, e.g. 5/30/2017 5:35:00 PM
Select the cell, click on the Format Painter icon (the paint brush)
Now click on the row header of the column that you want to apply the format.
This will copy a proper datetime format to the whole column, making it display correctly.

I know it is too late to answer to this question. But, I thought it would still be nice to share how I sorted this out when I had the same issue. Here is what I did.
Before copying the data, select the column in Excel and select 'Format cells' and choose 'Text' and click 'Ok' (So, if your SQL data has the 3rd column as DateTime, then apply this formatting to the 3rd column in excel)
Now, copy and paste the data from SQL to Excel and it would have the datetime value in the correct format.

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:
Copy range from SQL Server Management Studio
Paste into Excel
Select the range of cells that you need formatted as Dates
Press 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

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...

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

Here's a simple macro that can be run after pasting data from SSMS. It's easiest if you copy it to your PERSONAL.XLSB file and add a button to the Quick Access Toolbar or a new custom group/tab in the ribbon. Run the macro immediately after pasting while the data is still selected. It can also be run if a single cell is selected within the data - it will automatically select the current area before running (same as ctrl-a). To run the macro on only a subset of data, select the desired subset before running. It can handle the data including or excluding headers, but assumes there at at least 2 rows in the current area.
It efficiently tests each column to see if the first non-NULL value looks to be a date/time value in the strange format. If it is, it sets the entire column to the default system date/time format, even if your date format is 'd/m/y'.
Sub FixSSMSDateFormats()
'Intended for copied data from SSMS and handles headers included
'For selection or current area, checks each column...
' If the first non-NULL value is in strange time format, then change entire column to system date/time format
Dim values As Variant, r As Long, c As Long
If Selection.Count = 1 Then Selection.CurrentRegion.Select
values = Selection.Value
For c = 1 To UBound(values, 2)
For r = 2 To UBound(values, 1)
If TypeName(values(r, c)) = "Double" Then
If values(r, c) > 1 And Selection(r, c).NumberFormat = "mm:ss.0" Then
Selection.Columns(c).NumberFormat = "m/d/yyyy h:mm"
End If
Exit For
ElseIf values(r, c) <> "NULL" Then
Exit For
End If
Next
Next
End Sub

I've had this same problem for a while as I generate a fair number of ad-hoc reports from SQL Server and copy/paste them into Excel.
I would also welcome a proper solution but my temporary workaround was to create a default macro in Excel which converts highlighted cells to Excel's datetime format, and assigned it to a hotkey (Shift-Ctrl-D in my case).
So I open Excel, copy/paste from SSMS into a new Excel worksheet, highlight the column to convert and press Shift-Ctrl-D. Job done.

i've faced the same problem when copying data from ssms to excel. the date format got messed up. at last i changed my laptop's system date format to yyyy-mm-dd from yyyy/mm/dd. everything works just fine.

Related

Txt document data doesn't format correctly in Excel

Disclaimer - this should be a very simple task, but clearly everything I thought I knew about excel is false.
I'm trying to copy and paste data from a txt document to an excel document. An example of a line I'm trying to copy from the txt doc is: 4512544425701264.
However, when I paste into excel it pastes as 4512544425701260 but displays in the cell as 4.51254E+15. This happens for each line of data.
I've tried numerous ways to fix this problem none have worked including:
Copied the txt data into a Microsoft Word document, then tried
pasting into the excel.
Resized the cell.
Tried each cell formatting option.
Tried opening the txt doc in Excel, even progressed through the text import wizard. Interestingly, I noticed the data preview on page 3 displayed the data correctly. But after proceeding past the importer, the data loaded with the same problem.
Tried on another PC.
Copied and pasting one line at a time.
The only solution I have found is manually typing out each line, however this is highly unpractical due to the large amount of data.
Any help/advice would be greatly appreciated.
To show full number rightclick on cell, choose "Format cells..". In "Number" tab choose "Number" category and set "Decimal places" to zero.
Unfortunately excel lets to put only 15 digits as a number. Every additional number is converted to 0, that is why your number 4512544425701264 is converted to 4512544425701260
If you don't need this number to be used in calculation, you can format cells to text format before pasting:
Source on digits limitation: https://learn.microsoft.com/en-US/office/troubleshoot/excel/last-digits-changed-to-zeros
In Excel, this is expected behavior (it's Microsoft, after all). I have encountered it frequently when dealing with UPC codes, for example. In order to avoid this, format the cells you are trying to copy this data to as "Text" BEFORE you copy any data over.
Copy your data (CTRL+C), then select the first cell in the range where the data is going, then paste only the values. This should take care of it.
If for some reason you still get scientific notation, provided the cells have the "Text" formatting, you can select that cell, click in the Formula bar and hit ENTER.
When you paste into Excel, try calling "Text import wizard" in the Paste Options. This will allow you to set delimiter (in case of CSV) and later data type of a column where you can select "Text" which will tell wizard not try to convert your data into numbers.

Excel sheets In Selenium WebDriver

I have to read data from excel sheets to forms I automate using Webdriver2 and Testng but most of the time i am faced with the problem that the entered data in excel is not correctly put into forms while running the script.for e.g-
if I enter 2015 in excel( for an year field) it puts 1900 in form ,If I change the format of cell to text in excel, it works fine but then some other field is incorrectly entered.Like I had a scenario where the date field in form had format dd.mm.yyyy and the time had format hh:mm. So, it was entering correct values from excel( even though my excel lib code had format like dd/mm/yyyy) but for time field if i enter 31:12(any value greater than 24) it puts something else in form like 52:0.
I want to know, is there a way to format excel in such a way that it puts whatever data i put in cell regardless of format change.
I have tried changing the format , using regular expressions( works fine) and implemented suggestions from Web but sometimes nothing works.
Thanks in advance

excel cell need data in spreadsheet cell display, but excel uses other data displayed in input box only

I copied data from a callmanager. i want to use the data it displays in the spreadsheet which is hh:mm:ss. But in the input box in the top it shows other data (date and time which i think display the date and time of recording of the data). When I try to use to manipulate the hh:mm:ss in the spreadsheet it keeps using the data in te input box. How can I tell excel to use the data displayed in the spread sheet and remove the data in the input box?
thanks in advance!
I don't know of a specific method to do it in excel only, but what I've been using so far for instances like this is:
Copy the column having those values and paste into notepad,
Insert a new column in the worksheet and format it as Text,
Copy everything from the notepad and paste into that new column.
This might be a little awkward, but it works for me ^_^

Saving to CSV in Excel loses regional date format

I have a .xls I need to convert to .csv
The file contains some date columns. The format on the date is "*14/03/2001" which, according to Excel means the date responds to regional date and time settings specified for the OS.
Opening in Excel you see:
20/01/2013
01/05/2013
Save as... CSV
Open in notepad:
01/20/2013
05/01/2013
I have temporarily fixed by setting date formats to "14/03/2001" (no *) but even some other custom formats with no *, like "d/mm/yyyy h:mm" get mangled when saved to CSV.
Although keeping this in mind http://xkcd.com/1179/
In the end I decided to use the format YYYYMMDD in all CSV files, which doesn't convert to date in Excel, but can be read by all our applications correctly.
You can save your desired date format from Excel to .csv by following this procedure, hopefully an excel guru can refine further and reduce the number of steps:
Create a new column DATE_TMP and set it equal to the =TEXT( oldcolumn, "date-format-arg" ) formula.
For example, in your example if your dates were in column A the value in row 1 for this new column would be:
=TEXT( A1, "dd/mm/yyyy" )
Insert a blank column DATE_NEW next to your existing date column.
Paste the contents of DATE_TMP into DATE_NEW using the "paste as value" option.
Remove DATE_TMP and your existing date column, rename DATE_NEW to your old date column.
Save as csv.
Change the date and time settings for your computer in the "short date" format under calendar settings. This will change the format for everything yyyy-mm-dd or however you want it to display; but remember it will look like that even for files saved on your computer.
At least it works.
You need to do a lot more work than 1. click export 2. Open file.
I think that when the Excel CSV documentation talks about OS and regional settings being interpreted, that means that Excel will do that when it opens the file (which is in their "special" csv format). See this article, "Excel formatting and features are not transferred to other file formats"
Also, Excel is actually storing a number, and converting to a date string only for display. When it exports to CSV, it is converting it to a different date string. If you want that date string to be non-default, you will need to convert your Excel cells to strings before performing your export.
Alternately, you could convert your dates to the number value that Excel is saving. Since that is a time code, it actually will obey OS and regional settings, assuming you import it properly. Notepad will only show you the 10-digit number, though.
If you use a Custom format, rather than one of the pre-selected Date formats, the export to CSV will keep your selected format. Otherwise it defaults back to the US format
Place an apostrophe in front of the date and it should export in the correct format. Just found it out for myself, I found this thread searching for an answer.
A not so scalable fix that I used for this is to copy the data to a plain text editor, convert the cells to text and then copy the data back to the spreadsheet.
Change the date range to "General" format and save the workbook once, and change them back to date format (eg, numberformat = "d/m/yyyy") before save & close the book. savechanges parameter is true.
You can send your date to excel not like the "date" but like just a "string" with empty space at the start and end of the string.
Your problem is happening due to excel see your '11/02/2023' like a date, but you can hack it and change this date to a simple string like ' 11/02/2023 '.
Old: '11/02/2023'
New (solution): ' 11/02/2023 '

SSIS 2008, Excel 2007 formatting excel columns properly (Exporting, not importing)

I'm running into some formatting issues with exporting data to an excel 2007 spreadsheet.
I have a template file that has the columns formatted correctly, but when the data is exported it doesn't get the correct formats (currency, short date, etc.)
I'm thinking I have 2 options
Have excel autorun a macro. (hate that idea)
Format the data before it gets to excel and make it string data only. (I don't like that idea either as you are going to get those irritating data conversion arrows next to each cell)
I've used the IMEX=1 trick to get around the data import issues before, but is there a special trick for exporting?
thanks,
I spent ages trying to figurure this out.
What you need to do is have a hidden row of data in the spreadsheet you are exporting to.
SSIS will place the values into the spreadsheet, and assume the type is that of the row immediately above. So the data row needs to be the last row in the spreadsheet, and the data in each column needs to be of the type you want it to be.
You would think there was a better way of doing this. There isn't. Sorry :-)
So after much formatting and gnashing of teeth, I came down to the final solution. It appears that you still need the hidden row at the top for excel to use. That determines whether the data is formatted as numeric or string. If you want to do any more formatting, then you need to add in a script task to your SSIS package. I just threw the following lines into a .net script task at the end of my processing and it fixed it right up.
Please don't puke on the DailyWTF quality code ;-)
Dim wb As Microsoft.Office.Interop.Excel.Workbook
excel = New Microsoft.Office.Interop.Excel.Application
wb = excel.Workbooks.Open("c:\reports\Report.xlsx")
'This is the long ass Accounting Format'
wb.Worksheets("Sheet1").Columns("E:E").NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(#_)"
wb.Save()
wb.Close()
excel.Quit()
The solution is so simple it will make you scream. Just use the CREATE TABLE SQL command from a SQL task box in SSIS. Set the connection type to EXCEL. Here are the steps :
Let's use 'TEST' as the tab name on the spreadhseet.
1.) Copy the template file to a new file.
2.) Create a SQL task, set connection type to Excel and use the connection string to your newly created Excel file. Use the below code as a sample :
DROP TABLE TEST
GO
3.) Create another SQL task (using the same settings) and use the following code :
CREATE TABLE TEST
( Column1 Integer,
Column2 DATETIME,
Column3 NVARCHAR(50),
Column4 MONEY
)
GO
You can now start writing data. A couple of things I ran into is the I had to use the ` character instead of the ' for a comma. Also, I tried using both the DROP & CREATE commands in one SQL task but it didn't work for me, so I used two.

Resources