Unix Technical Forum

Cast to integer

This is a discussion on Cast to integer within the Pgsql General forums, part of the PostgreSQL category; --> Hello! Anyone could explain to me, why cast(3.33 to int) works (from float, I suppose) but cast('3.33' to int) ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 06:13 AM
Robert Osowiecki
 
Posts: n/a
Default Cast to integer

Hello!

Anyone could explain to me, why cast(3.33 to int) works (from float, I
suppose) but cast('3.33' to int) (from text) does not? And what if I
create a cast for that: is it possibly dangerous?

Regards,

Robert.


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 06:13 AM
Sean Davis
 
Posts: n/a
Default Re: Cast to integer

On 10/5/05 9:08 AM, "Robert Osowiecki" <robson@cavern.pl> wrote:

> Hello!
>
> Anyone could explain to me, why cast(3.33 to int) works (from float, I
> suppose) but cast('3.33' to int) (from text) does not? And what if I
> create a cast for that: is it possibly dangerous?


How about:

sdavis=# select (('3.33')::float)::int;
int4
------
3
(1 row)

Sean


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 06:13 AM
A. Kretschmer
 
Posts: n/a
Default Re: Cast to integer

am 05.10.2005, um 15:08:33 +0200 mailte Robert Osowiecki folgendes:
> Hello!
>
> Anyone could explain to me, why cast(3.33 to int) works (from float, I
> suppose) but cast('3.33' to int) (from text) does not? And what if I
> create a cast for that: is it possibly dangerous?


test=# select '3.33'::float::int;
int4
------
3
(1 row)



Regards, Andreas
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47212, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 06:13 AM
Tom Lane
 
Posts: n/a
Default Re: Cast to integer

Robert Osowiecki <robson@cavern.pl> writes:
> Anyone could explain to me, why cast(3.33 to int) works (from float, I
> suppose) but cast('3.33' to int) (from text) does not?


The latter is not really a cast; it's an invocation of the int
datatype's input function.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-09-2008, 06:14 AM
Robert Osowiecki
 
Posts: n/a
Default Re: Cast to integer

A. Kretschmer wrote:

>am 05.10.2005, um 15:08:33 +0200 mailte Robert Osowiecki folgendes:
>
>
>>Hello!
>>
>>Anyone could explain to me, why cast(3.33 to int) works (from float, I
>>suppose) but cast('3.33' to int) (from text) does not? And what if I
>>create a cast for that: is it possibly dangerous?
>>
>>

>
>test=# select '3.33'::float::int;
> int4
>------
> 3
>(1 row)
>
>
>

Let me explain better. I encountered this problem when I tried to write
general unit-calculation (from kilograms to punds, for example) function
operating on any arithmetic datatype: with float and numeric it went all
ok, but failed with integer on "return $result" with error "invalid
input syntax for integer". My function internally operated on numeric
type. I wrote a wrap-around for integer, because I've been afraid, that
changing "text to integer" cast (or input function, whatever it is)
could damage something important.

R.



---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:30 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com