Обсуждение: convert text to number or number to text

Поиск
Список
Период
Сортировка

convert text to number or number to text

От
JT Kirkpatrick
Дата:
I'm trying to do an update query that compares an int4 field to a variable 
which I think contains a string (it was passed in through cgi.pm).  can I 
convert that string to an integer? - it will always be a number passing 
through.  I vaguely remember oracle having a to_number function - does 
postgres have something similar?

jt



Re: [SQL] convert text to number or number to text

От
José Soares
Дата:
<tt>JT Kirkpatrick ha scritto:</tt><blockquote type="CITE"><tt>I'm trying to do an update query that compares an int4
fieldto a variable</tt><br /><tt>which I think contains a string (it was passed in through cgi.pm).  can I</tt><br
/><tt>convertthat string to an integer? - it will always be a number passing</tt><br /><tt>through.  I vaguely remember
oraclehaving a to_number function - does</tt><br /><tt>postgres have something
similar?</tt><tt></tt><p><tt>jt</tt></blockquote><tt>Trythis:</tt><tt></tt><p><tt>prova=> create table test(t text,
iinteger);</tt><br /><tt>CREATE</tt><tt></tt><p><tt>prova=> insert into test values('123',123);</tt><br /><tt>INSERT
645791</tt><tt></tt><p><tt>prova=> select cast(i as text),cast(t as int)from test;</tt><br /><tt>text|int4</tt><br
/><tt>----+----</tt><br/><tt> 123| 123</tt><br /><tt>(1 row)</tt><p>José <br />  

Re: [SQL] convert text to number or number to text

От
José Soares
Дата:
This should work:

select cast('123' as int), cast(123 as text);

José


JT Kirkpatrick ha scritto:

> I'm trying to do an update query that compares an int4 field to a variable
> which I think contains a string (it was passed in through cgi.pm).  can I
> convert that string to an integer? - it will always be a number passing
> through.  I vaguely remember oracle having a to_number function - does
> postgres have something similar?
>
> jt