dbTalk Databases Forums  

convert varchar field

comp.databases.mysql comp.databases.mysql


Discuss convert varchar field in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
remigio
 
Posts: n/a

Default convert varchar field - 10-21-2011 , 09:21 AM






Hi,
I'm administrating a mysql db via phpmyadmin and there's a VARCHAR field 'total' with some data stored in, i.e. 1432,26 (as Euro format).
I want to convert the VARCHAR type in DECIMAL or DOUBLE type because of some calculating problems, and I've tried to change the field's type (using phpmyadmin) from VARCHAR 16 into DECIMAL 16,2 but the result is 1432,00 and decimal ciphers were lost.
In which way can I change field format?
Thanks so much for help.

Remigio

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: convert varchar field - 10-21-2011 , 10:08 AM






On 2011-10-21 16:21, remigio wrote:
Quote:
Hi,
I'm administrating a mysql db via phpmyadmin and there's a VARCHAR field 'total' with some data stored in, i.e. 1432,26 (as Euro format).
I want to convert the VARCHAR type in DECIMAL or DOUBLE type because of some calculating problems, and I've tried to change the field's type (using phpmyadmin) from VARCHAR 16 into DECIMAL 16,2 but the result is 1432,00 and decimal ciphers were lost.
In which way can I change field format?
I'm only guessing here, but it might be the case that "," is not valid
as a decimal separator in your setup

create table T ( total varchar(10) not null ) engine = innodb;
insert into T (total) values ('1432,26'); -- ","
insert into T (total) values ('1432.26'); -- "."

mysql> select total, cast(total as decimal(6,2)) from T;
+---------+-----------------------------+
Quote:
total | cast(total as decimal(6,2)) |
+---------+-----------------------------+
1432,26 | 1432.00 |
1432.26 | 1432.26 |
+---------+-----------------------------+
2 rows in set (0.00 sec)

/Lennart

Reply With Quote
  #3  
Old   
Doug Miller
 
Posts: n/a

Default Re: convert varchar field - 10-21-2011 , 12:28 PM



On 10/21/2011 10:21 AM, remigio wrote:
Quote:
Hi,
I'm administrating a mysql db via phpmyadmin and there's a VARCHAR field 'total' with some data stored in, i.e. 1432,26 (as Euro format).
I want to convert the VARCHAR type in DECIMAL or DOUBLE type because of some calculating problems, and I've tried to change the field's type (using phpmyadmin) from VARCHAR 16 into DECIMAL 16,2 but the result is 1432,00 and decimal ciphers were lost.
In which way can I change field format?
You'll need to do this in stages:
1. Add a new column to the table, e.g.
ALTER TABLE mytable ADD COLUMN total_d DECIMAL (6,2);
2. Populate this column with data converted from VARCHAR to DECIMAL, e.g.
UPDATE mytable SET total_d = CAST (total AS DECIMAL);
3. *After* verifying that the new column has been updated correctly, get
rid of the VARCHAR column:
ALTER TABLE mytable DROP COLUMN total;
4. Rename the new column:
ALTER TABLE mytable CHANGE COLUMN total_d total;

Reply With Quote
  #4  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: convert varchar field - 10-25-2011 , 03:54 AM



El 21/10/2011 16:21, remigio escribió/wrote:
Quote:
I'm administrating a mysql db via phpmyadmin and there's a VARCHAR
field 'total' with some data stored in, i.e. 1432,26 (as Euro
format). I want to convert the VARCHAR type in DECIMAL or DOUBLE type
because of some calculating problems, and I've tried to change the
field's type (using phpmyadmin) from VARCHAR 16 into DECIMAL 16,2 but
the result is 1432,00 and decimal ciphers were lost. In which way can
I change field format?
A little hint:

SELECT
'1432,26',
CAST('1432,26' AS DECIMAL(20,2)),
REPLACE('1432,26', ',', '.'),
CAST(REPLACE('1432,26', ',', '.') AS DECIMAL(20,2))


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.