Re: Converting between varchar and float when updating
От | Thomas Larsen Wessel |
---|---|
Тема | Re: Converting between varchar and float when updating |
Дата | |
Msg-id | BANLkTim6nF_QjYxpHQErjhYwvkM-t+J0aA@mail.gmail.com обсуждение исходный текст |
Ответ на | Re: Converting between varchar and float when updating (Vibhor Kumar <vibhor.kumar@enterprisedb.com>) |
Ответы |
Re: Converting between varchar and float when updating
Re: Converting between varchar and float when updating |
Список | pgsql-general |
Thanks a lot :)
Both of the following work
UPDATE foo SET bar = (bar::float * 2);
removes trailing zeros on the decimal side, if no decimals dont show any "."
UPDATE foo SET bar = (bar::numeric * 2);
keeps decimals, i.e. 2.000 * 2 -> 4.000
That leads me to two additional questions:
1) Can I specify how many decimals I want to be stored back from the result? E.g. 2 / 3 = 0.66666666 but I want to just save 0.66.
2) Can I make a criteria that it should only update on the strings that can be converted. Maybe smth. like:
UPDATE foo SET bar = (bar::numeric * 2) WHERE bar::is_numeric;
Thomas
P.S.: Dmitriy asked why I save these values in VarChar. Well, I agree, that they should be numeric, but I did not design the schema which is btw 10 years old.
Both of the following work
UPDATE foo SET bar = (bar::float * 2);
removes trailing zeros on the decimal side, if no decimals dont show any "."
UPDATE foo SET bar = (bar::numeric * 2);
keeps decimals, i.e. 2.000 * 2 -> 4.000
That leads me to two additional questions:
1) Can I specify how many decimals I want to be stored back from the result? E.g. 2 / 3 = 0.66666666 but I want to just save 0.66.
2) Can I make a criteria that it should only update on the strings that can be converted. Maybe smth. like:
UPDATE foo SET bar = (bar::numeric * 2) WHERE bar::is_numeric;
Thomas
P.S.: Dmitriy asked why I save these values in VarChar. Well, I agree, that they should be numeric, but I did not design the schema which is btw 10 years old.
On Thu, Apr 28, 2011 at 12:18 PM, Vibhor Kumar <vibhor.kumar@enterprisedb.com> wrote:
Ah! Got it. This I have missed.
On Apr 28, 2011, at 3:41 PM, Dmitriy Igrishin wrote:
> Only one point, Vibhor. I believe that varchar data type was chosen for
> exact storage of numeric values. According to chapter 8.1.3 of the doc.
> for this case the usage of numeric is preferred over floating data types.
Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor.kumar@enterprisedb.com
Blog:http://vibhork.blogspot.com
В списке pgsql-general по дате отправления: