AutoSum and Sum return '0' when trying to add numbers imported by SQL - sql

Excel 2007 - anytime I click AutoSum it shows 0, or anytime I use the formula to Sum it still gives me a 0.
This is a SQL Query that is imported by using Data-Connection-SQL Server and saved SQL Query in the workbook.
What gives that is keeping me from totalling?

It seems to me the most likely explanation is that what you are trying to add are strings. If so, with Error Checking activated (Formulas tab) little triangles (by default green) should be clear evidence.
These also provide an easy way to convert strings that look like numbers into numbers that Excel can add. Select a contiguous range starting with a cell requiring conversion and a warning sign should appear:
(not always top left). Click on the exclamation mark and on Convert to Number and hopefully the format of all cells in your selection will be converted so your SUM function behave as you would like.
Much less likely (because I don't see how you would have done) is that somehow you have acquired leading spaces and when entering data it was into a cell formatted as Text. The triangle warning would be the same but Number Stored as Text does no conversion. A formula such as:
=TRIM(A1)
should do however, though it does depend on the type of space (eg would not work for NBSP - though you should not acquire one of those from an SQL import).
There may be other possible causes but it seems best to try the above before alternatives.

Related

Proper formatting of Excel sheets to avoid errors in SQL querying?

What do you avoid when creating and filling out a Excel spreadsheet of data for a SQL database (certain formats, characters, character length issues?)
2.Does it matter how dates are formatted?
VARCHAR or INTEGER errors you've seen?
Finally, what SQL or Python queries did you use to address errors you found that you might have shared for questions 1-3?
The easiest way would be, if you can import Database-EDI (e.g. Oracle SQL Developer) a TXT- or CSV-Excel-Export into our Database.
→ Depending on the database, different requirements must be observed.
The main focus is on the correct formatting with regard to the country settings (Excel & database):
Excel-Format-Date YYYY-M-DD HH24:MM / Databe-Timestamp YYYY-MM-DD HH24:MM:SS.FFFF
→ That would not work
In addition, make sure that Excel does not cut any numbers:
Excel-Format-Long-Number 89632150000 (orignal 896321512345 )
→ Excel automatically shortens the number in the standard settings.
The length of a text must not exceed the specified maximum length in the assigned column of the type (VARCHAR).
I think these would be the main points to look out for.

vba Excel to Access: zero length string to Null number

I have two values in the same column in Excel. I select one of them and run the following:
Debug.Print IsNumeric(Selection), _
VarType(Selection), _
VarType(Trim(Selection)), _
">" & Selection.Value & "<", _
Len(Trim(Selection)), _
Len(Selection), _
Selection.NumberFormat
Then I select the other and run the same debug.
And I get this:
True, 5, 8, >9.46979663546499<, 16, 16, General
False, 8, 8, ><, 0, 0, General
Note: the column has multiple occurrences of both
Can someone explain this? I've been vba'ing and Excel'ing a long time and I still don't get (in detail) the number formatting Excel does and how to work with them best. I think I have it then I always stumble upon something new like this.
In this case my objective is to get MS Access to automatically understand that this column is a double/number/float/whatever column that can be NULL when I import it and not throw errors. I have to achieve this through formatting/changing it in Excel prior to importing it. (Partly because that will work best with my client's processes and partly because I want to get my head around this finally...can't believe I don't already!) I have over 2000 rows to change for each column so a solution that formats the entire column at once would be best, not just one cell at a time.
Thanks!
IsNumeric returns true for the number and false for the blank. I'm not sure if this is unexpected, but MS had to make it return one or the other. The logic is that a blank is neither numeric or text.
Vartype returns Double for the number (as expected). If I VarType an empty cell, I get vbEmpty (0), not 8 as you get (Excel 2010 x86). If I put a single apostrophe in the cell, I get the same as you.
When you Trim() something, you convert it to text. It doesn't matter what you trim, the Trim function only returns a string, so you will always get VarType 8.
Read this post on mixed data types http://dailydoseofexcel.com/archives/2004/06/03/external-data-mixed-data-types/. Make sure you read the comments.
When an Office program imports, it uses some registry keys to determine data types. Typically, it reads the first 8 rows of the field to determine what the data type is. If it sees a mixture of data types, it picks the majority and converts or ignores everything else. You can change the registry settings to look at more than 8 rows or to default everything to text, but you can't tell it to treat empty cells as numbers.
It would be nice if it would simply ignore empty cells and take the majority of the rest. But 'empty cell' is just not a concept outside of Excel so it doesn't really surprise me.
The right answer for you, I think, is to create a Schema file and put it in the same directory as the file you're going to import. Read about it at https://msdn.microsoft.com/en-us/library/ms709353%28v=vs.85%29.aspx This is essentially setting all your column data types in a file.
I use this almost every day in Excel VBA - I use VBA to create a Schema.ini file, then use ADO to read in the file. I haven't ever used this in Access, particularly importing through the UI. But it's worth a try. If it doesn't work, you can just do all the importing yourself in VBA and ADO.
First, I would look logically at what type of data each column SHOULD contain. Some numbers are not to be calculated and therefore should be treated as text, especially in the case of (for example) item numbers with leading zeroes. You definitely DO NOT want to convert those to numbers and lose those leading zeroes, it will typically lead to downstream issues if whatever is querying them can't handle implicit conversion. Another example of this is where numbers exceed 15 in length. Excel will turn everything after the 15th digit to a zero because it's not a significant figure, but if this is a serial number (or the like) you are corrupting the data.
Once you understand what each column should be, use text-to-columns. Numbers should be general, dates should be dates, everything else should be text.
http://www.excel-easy.com/examples/text-to-columns.html
Text-To-Columns is superior for this purpose because it will actually convert the data type. If you use formatting it doesn't apply the formatting until you edit the cell.

Quickly Convert Text To Numbers or Dates Excel VBA

Is there any way to QUICKLY convert numbers/dates stored as text (without knowing exactly which cells are affected) to their correct type using VBA.
I get data in an ugly text-deliminated format, and I wrote a macro that basically does text-to-columns on it, but is more robust (regular text-to-columns will not work on my data, and I also don't want to waste time going through the wizard every time...). But, since I have to use arrays to process the data efficiently, everything gets stored as a String (and is thus transferred to the worksheet as text).
I don't want to have to cycle through every cell, as this takes a LONG time (these are huge data files - I need to use arrays to process them). Is there a simple command I can apply to the entire range to do this?
Thanks!
This has to do with the data type of the columns modify the column from general to the correct data type and the placement of text data should get automatically converted... here's an example where I pasted the text 012345 into different columns having different data types. Note how the displayed value is different for the different types but the value is retained (except on number and general which truncate a leading 0.
However if you don't know what field is of what type... you're really out of luck.
There is a way is there. Just multiply 1 with the data in the column have text to converted as number, whether it is text or not it will convert to numbers only.
Read the following the link for more.
http://chandoo.org/wp/2014/09/02/convert-numbers-stored-as-text-tip/

Exporting values from SQL Server to Excel

I live in Brasil and decimal separators are commas. For a bunch of reasons, I use dots as decimal separators in SQL Server, which is different from Excel.
With that being said, I would like to know why the following query
select 1.0*5
is understood as text in Excel (if so), when copying and pasting, and dots are not converted to commas, while
select cast(1.0*5 as float)
is understood as float in Excel.
What is the type of result in the first query?
UPDATE
If the query were
select 1.1*5
the result of copy and paste in Excel cell would be 5.5. It is not possible to convert this to value in Excel.
While the second query would result in 5,5. I can use the use this value in Excel in an addition operation, for example.
If you're doing it directly IN Excel, it seems that your regional settings are not seeing that as an operation with a decimal, but rather text. If you change your regional settings to US, it would probably resolve it correctly.
The difference between the two is that you are literally telling the value to be cast differently than the default. So your regional setting is overridden.
Excel, as smart as it is, tends to make many assumptions that could be tied to any number of things. Sometimes you just have to deal with it.
In the end, your 2nd query is likely to produce better results.

Preserve leading zeros when importing Excel into SQL

My office uses excel to prepare our data before importing it into a SQL database. However, we have been expreiencing the following error.
When the data is imported from one computer it loses all of the leading zeros. However, when it is imported from a different computer it imports perfectly.
An example of the leading zeros are that our item numbers are required to be formatted as "001, 002, 003,... 010, 011, 012,... 100, 101, 102, ect".
1) The excel file is stored on a server so there is no difference in the file.
2) If the users swap workstations the result stays with the computer, and doesn't switch with the user.
3) The data is formatted as text. It has been formatted as text both from the Data Tab and from Format Cells.
Is there a setting within excel that is specific to the computer and not the spreadsheet which will affect exporting the data? Or is there a non-excel specific setting which will cause this?
Its best to avoid the 'TEXT' format option. Confusingly, it does not force the contents of a cell to be a text data type, and it wreaks havoc when a formula references a 'TEXT' format.
To add to the previous answer (with all of the caveats about if this is a good idea), you can use the TEXT worksheet function
=TEXT(A1,"000")
to guarantee an actual text string with leading zeros if needed.
Depending on number of leading zeroes that you require, you can select your data/column in Excel, go into Excel >> Format >> Custom >> type in however many zeroes you require into the Type field (i.e. 000000000 for a 9-digit number with leading zeroes), and it will automatically preface with the correct number of leading zeroes to make the numerical string the correct length (i.e. 4000 = 00004000).
Note, this only works with numerical data, not text, but depending on the scenario it may be more useful to retain your data in numerical format - the example you gave listed numerical data only, and often retaining the numerical format is a benefit for analysis.
Not sure what the benefit of padding data before inserting it into the database would be...(takes more space, slower searching, etc.). Sounds like you're formatting it for output (?), which might be more efficiently done elsewhere.
But anyway -- here are some ideas for your SELECT (sql) statement:
RIGHT(1000 + [excel field], 3)
or another one would be
REPLICATE('0', 3 - LEN([excel field])) + [excel field]
Something you can do to the Excel field itself (before import) is prefix it with a ' (apostrophe). Notice if you type 0007 into Excel, it will change it to 7, but if you type '0007, it will keep the leading zeros.