when i get data from database and export it as a csv file i have following issues :
the leading zero of the numbers dosnt show
big numbers shows like this -> 1E+12
how to force MS Excel to show all of my data as string by PHP
You can use double quotes contain the numbers, like this:
if(is_numeric($column)) $column = '"'.$number.'"';
This isn't a MySQL issue, it's an Excel thing.
This... might help: http://answers.microsoft.com/en-us/office/forum/office_2007-excel/disabling-scientific-notation/943b8103-8c50-451d-8037-c697422e2307
But this question is more MS Office related than programming.
(or as comments suggested, use a file format that carries display formatting info, such as .xls)
I format it as a string by concatenate it with spaces at its start and end.
CONCAT(" ", database_number, " ") AS "Number For Excel".
Kai's answer didn't quite work for me. I ended up CONCATing an equals sign (still with the double quotes on either end) to the front and got the desired result. Our users wanted to be able to copy and paste the number straight out of Excel into our UI.
CONCAT('="',too_long_number_field,'"')
You can use number_formate() function to convert ...
View link
Function : number_format()
Syntax : number_format ( float $number , int $decimals = 0 , string $dec_point = ‘.’ , string $thousands_sep = ‘,’ )
Example to convert 6.90743E+11 to number use below code
number_format(6.90743E+11,0,'','') // outputs 690743000000
I have a csv in .txt who need to be imported to MySql
I'm importing it manualy to my table but it keep breaking giving me this error :
Invalid column count in CSV input on line 706.
checking it on excel the only information who could make the problem is that one of the cells contains : 5.5 x 18"
probably the quotation mark is doing it
how can i fix this for the import ?
thank you
I believe all you need is this:
str_replace('"', '\"', $value);
By replacing the " with \" you are escaping it and it should insert properly.
How do we deal with field with comma when using load data infile? i have this query:
$sql = "LOAD DATA LOCAL INFILE '{$file}' INTO TABLE sales_per_pgs
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#user_id, #account_code, #pg_code, #sales_value)
SET
user_id = #user_id,
account_code = #account_code,
product_group_code = #pg_code,
sales_value = REPLACE(#sales_value, ',', ''),
company_id = {$company_id},
year = {$year},
month = {$month}";
and a line from the csv looks like this:
139, pg89898, op89890, 1,000,000.00
where 1,000,000.00 is a sales value.
Currently, what is inserted in my database is only "1.
EDIT
The user downloads a form with columns like:
user id, account id, pg id, sales value
where the first three columns user id, account id, pg id, were populated and the sales value column is blank because the user has to fill it up manually... the user uses MS excel to do that...
after the form is completed, he will now upload it, in which i am using the load data infile command...
Your content should really look like:
"139", "pg89898", "op89890", "1,000,000.00"
Then you could add the following to the command:
ENCLOSED BY '"' ESCAPED BY "\\"
And you won't have an issue.
Also, somethign you could try if you don't have any paragraphs or strings with , in them:
FIELDS TERMINATED BY ', '
You will have to alter the CSV file that is being input or alter the output that generates the CSV file - sounds the same but it isn't.
You can modify the data coming in by encapsulating fields with quotes and update your command so that it recognizes that fields are encapsulated with them using a command like ENCLOSED BY '"'
or
alter your output so that it formats the number as 1000000 rather than 1,000,000
had the same problem and used just ENCLOSED BY '"' which fixed my issue since i had mixed numbers and strings which is exctyly what ENCLOSED BY is for , from the manuall :
If you specify OPTIONALLY, the ENCLOSED BY character is used only to
enclose values from columns that have a string data type (such as
CHAR, BINARY, TEXT, or ENUM):
In a CSV, comas separate "columns". Since your last value is 1,000,000.00 it is regarded as 3 different columns instead one just one (as intended).
You can either quote each value(column) or change the number format, by removing the commas (,).
if your entire file is exactly as you wrote, then maybe you could use fields terminated by ', ' (comma + space), if and only if you don't have that string within any individual value. If you are using Linux (or any other Unix like system) and your field separator is comma + space, you can use sed to replace this separator with something else:
sed 's/, /|/g' myfile.csv > myfile.txt
However, I would recommend what has already been said: modify your input file enclosing each value with quotes or double quotes and use fields terminated by ',' optionally enclosed by '"'.
Remember that your field termination character must be unique, and must not be contained within any individual value.
As a workaround, try this one -
LOAD DATA INFILE
...
FIELDS TERMINATED BY ', '
...
Anyone know how to insert bar/pipe delimited array in an array field?
With comma its working but when I change it to bar it produces error "malformed array literal"
example (works):
insert into table (arrayfield) values ('{"var1","var2","var3"}')
example (doesn't work):
insert into table (arrayfield) values ('{"var1"|"var2"|"var3"}')
btw i'm using postgres 8.2 and PHP and i can't use comma as delimiter for various reasons.
If you can use values without quotes because you know that no pipe is in your data, then you can use:
insert into table (arrayfield) select string_to_array('var1|var2|var3','|');
If you need that your values are quoted on your pipe-delimited string then it gets complicated. If that's the case then I think you should format and quote properly your array using ARRAY['val1','val2','val3'] syntax in PHP.
I have an array of values, and one of the values is another array (see below). I implode the inner-array so that it becomes v0,v1,vx and I'm inserting it into a column of a mysql database where the datatype is SET()
a = array(
"first"=>"foo",
"second"=>array("b","a","r"), // this becomes "second"=>"b,a,r",
"third"=>"bang"
)
My question is which PDO::PARAM_* should I use?
(Initially I would think PARAM_STR, but I'm not sure if PDO will do something that won't work with SET()).
PARAM_STR should do the job correctly. It will escape values and add single quotes around comma separated list.