Skip site navigation (1) Skip section navigation (2)

Re: Converting between varchar and float when updating

From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Thomas Larsen Wessel <mrvelle(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Converting between varchar and float when updating
Date: 2011-04-28 09:41:56
Message-ID: BANLkTikHTQnDTfmnaqeRyCTxJcweTi5_xw@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-general
On 28 April 2011 11:26, Thomas Larsen Wessel <mrvelle(at)gmail(dot)com> wrote:

> I have a table with the following schema:
> CREATE TABLE foo (bar VARCHAR(32));
>
> Every bar value has a format like a float, e.g. "2.5". Now I want that
> value multiplied by two and saved again as varchar. I was hoping to do smth
> like:
>
> UPDATE foo SET bar = TO_VARCHAR( TO_FLOAT(bar) * 2); -- INCORRECT!!!!
>
> How is that done?
>
> I know that the bar attribute ought to have type FLOAT, but I have to work
> with this legacy database. And anyway this table will rarely be updated.
>
> Sincerely, Thomas
>
>
Hi,
maybe try this instead:

UPDATE foo SET bar = (bar::float * 2) :: text;

regards
Szymon

In response to

pgsql-general by date

Next:From: Vibhor KumarDate: 2011-04-28 09:49:57
Subject: Re: Converting between varchar and float when updating
Previous:From: tamanna madaanDate: 2011-04-28 09:41:28
Subject: Re: setting connection/ query timeout

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group