How do I remove characters from a query field that cause Excel to interpret the field as more than one column or as a function - excel

I am stuck having to query a SQL Server database that is mimicking SQL server 2000 database and no way around it.
I have a large result set of 5 fields. The last field is a memo field. The result set is so large in SSMS 2012 that I cannot select them all with headers. So I have to save to Excel csv format. In doing so it interprets data in the 5th field as either a function (“-“, “+”, “(space) –“, “(space)+”, etc at the beginning) or as multiple columns for various reasons.
So far I have
replace(ltrim(rtrim(memo)), ',', ' ') as Memo
This, of course, trims beginning and end and replaces commas with spaces. I do not want to have to build nested replaces unless I must. This is for a large audit report that is not run often so I can, if need be, use a function.
Is there a good way to make a field like this compliant with Excel so that Excel will just keep that field as one column? I would appreciate any insight.

It seems that the correct method is to append double quotes to the beginning and the end of the field value returned in the query. As I am having to right-click and output to Excel this methods works and Excel does not misinterpret the intent.

Related

When saving excel file, numbers are converted to date

I have a file (only one) with some columns with integer numbers. When I change and save file, those columns are automaticaly converted in dates. Does anyone knows how can i prevent this? Thank You!
you might want to check out a similar issue here:
http://www.mrexcel.com/forum/excel-questions/637277-excel-converting-numbers-dates.html
(the below suggestions might not be particularly relevant to this issue, but in general, they might help to resolve such a problem)
Selct the cells and go to Format --> cells --> number and select Text for the selection
There's also a one-keystroke solution: type an apostrophe before entering or pasting a pair of numbers that Excel could mistake for a date and month. When you exit the cell, the apostrophe vanishes and the numbers stay numbers, formatted as text.
this is what microsoft says:
http://office.microsoft.com/en-001/excel-help/stop-automatically-changing-numbers-to-dates-HA102809473.aspx
also if you are pasting data from somewhere else
try Paste Special
I guess there is still no solutions to this problem.
It happens in both my computers when inserting data from sql server 2008.
Half of columns becomes dates..... and im supposed to give this to my accountant.. sucks
(Btw i know about the special paste to text, then convert to number... but it takes so much time)....
I just ran into this issue. I am using a webhook to store data to excel on the fly, adding new rows, updating existing data, etc.
I had a special field "step" which is a string who can be "1", "1.1", "1.2", etc. which was treated as date as soon as the value was a "string decimal"...
The only way I could find to fix this issue was to programmatically add a ' before the value, like '1.2.
I tried to apply this to all my fields to avoid excel side effect, but I then noticed that when updating an existing row by merging new values with existing values then the ' was stripped. So, I end up using a whitelist of fields to escape. I couldn't locate the fields to force adding ' when merging data due to limitations from the lib I use google-sheets-node-api.

Powerbuilder - Keep Column names when saving as Excel format

I am kind of new to PowerBuilder and I'd like to know if it was possible to keep the "visible" value of a column name when using the SaveAs() Method of my DataWindow. Currently, my report shows columns like "Numéro PB" or "Poste 1-3", but when I save, it shows the Database's names. ie: "no_pb" and "pos_1_3"...
As I am working on a deployed application, I have to make my changes and implementations As user-friendly as possible, and they won't understand anything of that.
I already use the dw2xls api to save an exact copy of the report, but they want to have an option saving only the raw data, and I don't think I can achieve it using their API.
Also, I was asked not to use the Excel OLE object to do it...
Anyone's got an idea?
Thanks,
Michael
dw.saveas(<string with filename and path>,CSV!,TRUE) saves the datawindow data as a comma separated value text file with the first row having the column headers (database names in the dw painter).
To set the column headings in a saveas you could first access the data with
any la_dwdata[] // declare array
la_dwdata = dw_1.Object.Data // get all data for all rows in dw_1 in the Primary! buffer
from here you would create an output file consisting initially of a series of strings along
with the column names you want and then the data from the array converted to a string (you loop through the array). If you insert commas between the values and name the file with the 'CSV' extension, it will load into Excel. Since this approach will also include any non visible data, you may have to use other logic to exclude them if the users don't want to see it.
So now you have a string consisting of lines of data separated by tabs along with a crlf at the end of each. You create your 'header string' with the user friendly column names in the format of 'blah,blah,blah~r~n' (this is three 'blah' strings separated by commas with a crlf at the end).
Now you parse the string obtained from dw_1.Object.Data to find the first line, strip it off, then replace it with the header string you created. You can use the replace method to replace the remaining tabs with a comma. Now you save the string to a file with a .CSV extension and you can load it into Excel
This assumes that your display columns match your raw columns. Create a DataStore ds_head . Set your report DW as the DataObject (no data). I'm calling the DataWindow with the report you want to save dw_report. You'll want to delete the two temporary files when you're done. You may need to specify EncodingUTF8! or some other encoding instead of ANSI depending on what the data in the DataWindow is. Note: Excel will open this CSV but some other programs may not like it because the header row has a trailing comma.
``
ds_head.saveAsFormattedText("file1.csv", EncodingANSI!, ",")
dw_report.saveAs("file2.csv", CSV!, FALSE, EncodingANSI!)
run("copy file1.csv file2.csv output.csv")

Export data from Access to Excel without losing leading zeroes

I have a table in Access I am exporting to Excel, and I am using VBA code for the export (because I actually create a separate Excel file every time the client_id changes which creates 150 files). Unfortunately I lose the leading zeroes when I do this using DoCmd.TransferSpreadsheet. I was able to resolve this by looping through the records and writing each cell one at a time (and formatting the cell before I write to it). Unfortunately that leads to 8 hours of run time. Using DoCmd.TransferSpreadsheet, it runs in an hour (but then I lose the leading zeroes). Is there any way at all to tell Excel to just treat every cell as text when using the TransferSpreadsheet command? Can anybody think of another way to do this that won't take 8 hours? Thanks!
prefix the Excel value with an apostrophe (') character. The apostrophe tells Excel to treat the data as "text".
As in;
0001 'Excel treats as number and strips leading zeros
becomes
'0001 'Excel treats as text
You will probably need to create an expression field to prefix the field with the apostrophe, as in;
SELECT "'" & [FIELD] FROM [TABLE]
As an alternative to my other suggestion, have you played with Excel's Import External Data command? Using Access VBA, you can loop through your clients, open a template Excel file, import the data (i.e. pull instead of push) with your client as a criteria, and save it with a unique name for each client.
What if you:
In your source table, change the column type to string.
Loop through your source table and add an "x" to the field.
If the Excel data is meant to be read by a human being, you can get creative, like hiding your data column, and adding a 'display' column that references the data column, but removes the "x".

SSIS Excel Data Source - Is it possible to override column data types?

When an excel data source is used in SSIS, the data types of each individual column are derived from the data in the columns. Is it possible to override this behaviour?
Ideally we would like every column delivered from the excel source to be string data type, so that data validation can be performed on the data received from the source in a later step in the data flow.
Currently, the Error Output tab can be used to ignore conversion failures - the data in question is then null, and the package will continue to execute. However, we want to know what the original data was so that an appropriate error message can be generated for that row.
According to this blog post, the problem is that the SSIS Excel driver determines the data type for each column based on reading values of the first 8 rows:
If the top 8 records contain equal number of numeric and character types – then the priority is numeric
If the majority of top 8 records are numeric then it assigns the data type as numeric and all character values are read as NULLs
If the majority of top 8 records are of character type then it assigns the data type as string and all numeric values are read as
NULLs
The post outlines two things you can do to fix this:
First, add IMEX=1 to the end of your Excel driver connection string. This will allow Excel to read the values as Unicode. However, this is not sufficient if the data in the first 8 rows are numeric.
In the registry, change the value for HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Nod\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows to 0. This will ensure that the driver looks at all the rows to determine the data type for the column.
Yes, you can. Just go into the output column list on the Excel source and set the type for each of the columns.
To get to the input columns list right click on the Excel source, select 'Show Advanced Editor', click the tab labeled 'Input and Output Properties'.
A potentially better solution is to use the derived column component where you can actually build "new" columns for each column in Excel. This has the benefits of
You have more control over what you convert to.
You can put in rules that control the change (i.e. if null give me an empty string, but if there is data then give me the data as a string)
Your data source is not tied directly to the rest of the process (i.e. you can change the source and the only place you will need to do work is in the derived column)
If your Excel file contains a number in the column in question in the first row of data, it seems that the SSIS engine will reset the type to a numeric type. It kept resetting mine. I went into my Excel file and changed the numbers to "Numbers stored as text" by placing a single quote in front of them. They are now read as text.
I also noticed that SSIS uses the first row to IGNORE what the programmer has indicated is the actual type of the data (I even told Excel to format the entire column as TEXT, but SSIS still used the data, which was a bunch of digits), and reset it. Once I fixed that by putting a single-quote in my Excel file in front of the number in the first row of data, I thought it would get it right, but no, there is additional work.
In fact, even though the SSIS External DataSource Column now has the type DT_WSTR, it will still read 43567192 as 4.35671E+007. So you have to go back into your Excel file and put single quotes in front of all the numbers.
Pretty LAME, Microsoft! But there's your solution. I have no idea what to do if the Excel file is not under your control.
I was looking for a solution for the similar issue, but didn't find anything on the internet. Although most of the found solutions work at design time, they don't work when you want to automate your SSIS package.
I resolved the issue and made it work by changing the properties of "Excel Source". By default the AccessMode property is set to OpenRowSet. If you change it to SQL Command, you can write your own SQL to convert any column as you wish.
For me SSIS was treating the NDCCode column as float, but I needed it as a string and so I used following SQL:
Select [Site], Cstr([NDCCode]) as NDCCode From [Sheet1$]
Excel source is SSIS behaves crazy. SSIS determines the type of data in a particualr column by reading first 10 rows.. hence the issue. If you have a text column with null values in first 10 roes, SSIS takes the data type as Int. With a bit of struggle, here is a workaround
Insert a dummy row (preferrably first row) in the worksheet. I prefer doing this thru a Script task, you may consider using some service to preprocess the file before SSIS connects to it
With the duummy row, you are sure that the datatypes will be set as you need
Read the data using Excel source and filter out the dummy row before you take it for further processing.
I know it is a bit shabby, but it works :)
I could fix this issue. while creating the SSIS package, I manually changed the specific column to text (Open the excel file select the column, right click on column, select format cells, in number tab select Text and save the excel).
Now create the SSIS package and test it. It works. Now try to use the excel file where this column was not set as text.
It worked for me and I could execute the package successfully.
This should be resolved simply, just untick the box "Frist row as column names" and all data will be collected as text data type. Only downside of this choice is that you have to manage the columns names from the auto names (column 1, 2 etc) and handle the first row which contains the column names.
I had trouble implementing the solution here - I could follow the instructions, but it only gave new errors.
I solved my conversion issues by using a Data Conversion entity. This can be found on the SSIS Toolbox under Data Flow Transformations. I placed the Data Conversion between my Excel Source and OLE DB Destination, linked Excel to Data C, Data C to OLE DB, double clicked Data C to bring up a list of the data columns. Gave the problem column a new Alias, and changed the Data Type column.
Lastly, in the Mappings of the OLE DB Destination, use the Alias column name, rather than the original Excel column name. Job done.
You can use a Data Conversion component to convert to the desired data types.

stop Ms Excel auto-formatting numeric strings as numbers

I am exporting a report from MS Access(2003) to Excel (97-2003) output.
One of the columns has a character string that is numeric for some rows e.g. "05-0880".
When I open the output file in MS Excel the corresponding cell is set to the number -372424.
I assume this is caused by Excel being "clever" and deciding that "05-0808" represents a time value or date of some sort and converting the string to a corresponding numeric value.
In my case the data represents product codes and this conversion is very undesirable.
If I export the output of the report's underlying query (exporting a Datasheet) this conversion does not happen. (Suggesting that it must be possible for Access to output to Excel format in a way that prevents conversion).
I tried changing the format attribute of the field in the report, but there is no "Text" option, and "Standard" seems to behave the same as leaving it blank.
Is there a way to use the format() function to force string output?
If so, will this even help when Excel may still do its clever conversion?
Just using the underlying query as output is an option but not ideal because formatting and grouping are lost. Exporting a report to a text format is a disaster because the columns are not delimited.
Actually, it is more like the other way around. MS Access is too dumb to export it properly from a report. You can armor the data with quotes or apostrophes; however, as you know, that's quite ugly and requires post processing.
My way around it was to carefully craft a query and then export the results of the query rather than the report. This will give you the results you expect.
Export "'05-0880" instead of "05-0880".
make sure you use the single quote not the other apostrophe.

Resources