Pasting SQL decimal columns into Excel - teradata

I have a issue with data formats of Excel and SQL.
I have a column in SQL which is of datatype DECIMAL(18,0) and when I am trying to paste the result in SQL..the last 3 digits of the sql result gets replaced by 0 in Excel.
Example:
In SQL the result set has a column called session id and has decimal numbers like
119,597,417,242,309,670
329,621,151,415,350,454
134,460,940,261,658,890
but when I paste it in Excel the numbers look like:
I tried changing the format in EXCEL to paste as text however, the whole format of the result set gets distorted (and only the first column gets pasted properly without the 0's)
I can't keep casting all columns in SQL from decimal to int as there are way too many columns.
Can you please guide me as to what I can do?

Numeric fields in Excel are limited to 15 digits precision.
In SQL Assistant under Tools / Options / Data Format you can ask to have large Decimal (and BIGINT) fields displayed as text for just this sort of copy / paste. Or you can tell SQL Assistant to Save As or Export to Excel format.
For other tools you can explicitly FORMAT and CAST the data to VARCHAR in your SELECT so it is retrieved as text.

Several things you can do. I'll list 4.
Pick whatever suits you best.
First paste in a text editor (like notepad), seach/replace there, and paste that.
Set the datarange where you're going to paste to "text", and then paste. After that you can search/replace, and change to the correct format.
Change the regional settings of Windows to match the data that you have.
You can generate formula's from your SQL query, instead of floating point numbers. So generate a text like =5/10 instead of 0.5 or 0,5. Excel will pick it up correctly regardless of your regional settings.

Related

Add leading zeros to a character variable in progress 4gl

I am trying to import a .csv file to match the records in the database. However, the database records has leading zeros. This is a character field The amount of data is a bit higher side.
Here the length of the field in database is x(15).
The problem I am facing is that the .csv file contains data like example AB123456789 wherein the database field has "00000AB123456789" .
I am importing the .csv to a character variable.
Could someone please let me know what should I do to get the prefix zeros using progress query?
Thank you.
You need to FILL() the input string with "0" in order to pad it to a specific length. You can do that with code similar to this:
define variable inputText as character no-undo format "x(15)".
define variable n as integer no-undo.
input from "input.csv".
repeat:
import inputText.
n = 15 - length( inputText ).
if n > 0 then
inputText = fill( "0", n ) + inputText.
display inputText.
end.
input close.
Substitute your actual field name for inputText and use whatever mechanism you are actually using for importing the CSV data.
FYI - the "length of the field in the database" is NOT "x(15)". That is a display formatting string. The data dictionary has a default format string that was created when the schema was defined but it has absolutely no impact on what is actually stored in the database. ALL Progress data is stored as variable length length. It is not padded to fit the display format and, in fact, it can be "overstuffed" and it is very, very common for applications to do so. This is a source of great frustration to SQL reporting tools that think the display format is some sort of length limit. It is not.

Is it possible to remove the binary values that prefix the output of teradata fexp?

I am trying to run teradata fexp with a simple sql script.
The select output column is a string expression and as such results in 2 extra length indicator bytes at the start of each row output.
I have searched for solutions online to the problem. I would like to avoid having to post-process if possible.
There is a thread suggesting the possibility of using an OUTMOD. I don't know what that is.
https://forums.teradata.com/forum/tools/fastexport-remove-binaryindicator-values-in-outmod
http://teradataforum.com/teradata/20100726_155313.htm
And yet another thread suggests casting to a fixed width string type but this would result in padding which I'd like to avoid.
https://forums.teradata.com/forum/tools/fexp-data-doubt
The desired output is actually a delimited plain text file. Is there a way to do it?

how we can change the format of specific cell to our desired format before taking cell content phpexcel?

I should read data from more than 4 different excel file with different cell formating but same data within, so how i can change the cell format then read the data using phpexcel?
If you're storing a numeric value that's longer than a 32-bit signed integer can handle (such as 435546567567345) then treat it as a string using
$objPHPExcel->getActiveSheet()
->setCellValueExplicit(
'A1',
'435546567567345',
PHPExcel_Cell_DataType::TYPE_STRING
)
EDIT
If you're reading this value from an Excel worksheet, and it is actually a number value rather than a string containing a numeric value, then it is likely being treated as a float by MS Excel, so there may well be some loss of precision already (unless the file was created using a 64-bit version of MS Excel), even before PHPExcel reads it. If it is a number created using a 64-bit version of MS Excel, then you'll need a 64-bit version of PHP to read it without loss of precision.
Try reading the raw, unformatted value using getValue() and then doing a var_dump() to see what datatype it actually is; or try using getDataType() to see what the value was being stored as in the Excel file

PHPExcel Accounting Formats

I'm working with PHPExcel and I'm trying to format a cell using Excel's built-in "Accounting" format. I'm aware of the Format Code:
PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE
But this simply formats to two decimal places and adds a $ in front of the number. The result I'm looking for is the right aligned cell with the $ on the left. $0 values should be listed as "-" and negative values should be $ (1.11)
As far as I can tell there are no other currency formats in the documentation (though I may have missed it, this documentation is horrendous). Am I looking in the wrong place? Can this be achieved with regular cell formatting or is Excel doing something unique for Accounting?
I reverse engineered the format code from an existing spreadsheet using PHPExcel and got this:
_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(#_)
This is the code Excel places on the cell when you select the "Accounting" format... or click that "$" toolbar button in Excel 2007.
If you didn't need currency symbol :
->setFormatCode("_(* #,##0.00_);_(* \(#,##0.00\);_(* \"-\"??_);_(#_)");
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#,##0.00");
or use
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#.##0,00");
Set in getStyle CELL.
There are no other pre-defined formats beyond those listed in PHPExcel_Style_NumberFormat, but you should be able to set the format code to any string that you could use when setting an MS Excel custom format...
e.g.
[green]$#,##0.00;[red]$(-#,##0.00)
As regards cell alignment, set this to right yourself, or don't set it at all.

Integer zero, "0' will be ignored when upload to SQL Server

i have a page that allow user to upload an excel file and insert the data in excel file to the SQL Server. Now i have a small issue that, there is a column in excel file with values, such as "001", "029", "236". When it's insert to the SQL Server, the zero in front will be ignored in SQL, so the data would become "1", "29", "239". The data type for the column in SQL is varchar(10). How to solve this?
Excel seems to automatically convert cell values to numbers. Try prefixing the cell contents with a single quote in the Excel sheet prior to processing. Eg '001. If you can't trust the users to do that, use a string formatting routine to left pad the numbers with zeroes.
Something must be converting the data in the excel cell from a string to an integer. How are you performing the insert?
If a user enters 001 into Excel, it will be converted to the number 1.
If the user enters '001 into Excel, it will be saved in the cell as text.
If the cell is pre-formatted with the number format "#", then when the user enters 001 into the cell it will be entered as the text "001". The "#" number format tells Excel that the cell is a text cell and any entry (whether it looks like a number, date, time, fraction, etc...) should simply be placed in the cell as is - as a text cell.
Can you tell your users to pre-format this column with "#"? This is generally the most reliable way to handle this since the user does not have to remember to enter '001.
Maybe setting up the datatype "Text" for an Excel cell will help.
Excel is probably the culprit here. Try converting your file to CSV and see how it comes out. If the leading zeros are gone in the new CSV file, Excel is the problem.
Excel always does this, and its a nuissance. There are three workarounds I know of:
BEFORE entering the data in any cell in Excel format the cell as text (you can do a whole column if needed.) This only works if you control the spreadsheets and users, which is basically never :-).
Assume you'll get a mix of numbers and/or text in the Excel data, and fix it in Excel before import: add a column to the spreadsheet and use the TEXT() function to convert the number to text, as in =TEXT(A2, "000"); fill down. Also assumes you can edit the worksheet.
Assume you have to fix the numbers upon insert in your code. Depending on how you are loading the data, that could happen in T-SQL or in your other code. In TSQL this expression works to pad with zeros to a width of 3 characters: right( '000' + cast ( 2 as varchar(3) ), 3 )

Resources