dbTalk Databases Forums  

"explicit type casts" ?

comp.databases.postgresql comp.databases.postgresql


Discuss "explicit type casts" ? in the comp.databases.postgresql forum.



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

Default "explicit type casts" ? - 08-06-2011 , 02:56 PM






I have a table with one column as follows:

=# \d+ procedures
Table "public.procedures"
Column | Type | Modifiers | Storage | Description
-------------------+-------------------+-----------+----------+-------------
runway | character varying | | extended |


Now:

SELECT airport,ident,runway FROM procedures WHERE runway = 35;

ERROR: operator does not exist: character varying = integer
LINE 1: ...LECT airport,ident,runway FROM procedures WHERE runway = 35;
^
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.


I tried
WHERE runway = '35';
WHERE runway = "35";
WHERE runway LIKE '35';
WHERE runway LIKE '%35%';
WHERE runway LIKE '35%';

Last one works but why? and what is it all about?

How does one make a sure-fire workaround to this problem?

/Bjarne

Reply With Quote
  #2  
Old   
Robert Klemme
 
Posts: n/a

Default Re: "explicit type casts" ? - 08-07-2011 , 06:06 AM






On 06.08.2011 21:56, Bjarne Jensen wrote:
Quote:
I have a table with one column as follows:

=# \d+ procedures
Table "public.procedures"
Column | Type | Modifiers | Storage | Description
-------------------+-------------------+-----------+----------+-------------

runway | character varying | | extended |


Now:

SELECT airport,ident,runway FROM procedures WHERE runway = 35;

ERROR: operator does not exist: character varying = integer
LINE 1: ...LECT airport,ident,runway FROM procedures WHERE runway = 35;
^
HINT: No operator matches the given name and argument type(s). You might
need to add explicit type casts.


I tried
WHERE runway = '35';
WHERE runway = "35";
WHERE runway LIKE '35';
WHERE runway LIKE '%35%';
WHERE runway LIKE '35%';

Last one works but why? and what is it all about?
Maybe you have trailing whitespace in the column value.

Quote:
How does one make a sure-fire workaround to this problem?
How about a real solution: storing numeric values in numeric types?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Reply With Quote
  #3  
Old   
Bjarne Jensen
 
Posts: n/a

Default Re: "explicit type casts" ? - 08-07-2011 , 11:44 AM



On 2011-08-07 13:06, Robert Klemme wrote:

Quote:
How about a real solution: storing numeric values in numeric types?

Great idea, but I cannot control it - it is a vendor supplied database.
I'm just trying to work with it.

/Bjarne

Reply With Quote
  #4  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: "explicit type casts" ? - 08-07-2011 , 12:37 PM



On Sat, 06 Aug 2011 21:56:52 +0200, Bjarne Jensen wrote:

Quote:
SELECT airport,ident,runway FROM procedures WHERE runway = 35;

ERROR: operator does not exist: character varying = integer LINE 1:
...LECT airport,ident,runway FROM procedures WHERE runway = 35;
^
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.


I tried
WHERE runway = '35';
WHERE runway = "35";
WHERE runway LIKE '35';
WHERE runway LIKE '%35%';
WHERE runway LIKE '35%';

Last one works but why? and what is it all about?

How does one make a sure-fire workaround to this problem?

/Bjarne
This may help:


[mgogala@medo ~]$ psql
psql (9.0.2)
Type "help" for help.

mgogala=# create temporary table procedures(runway varchar(10));
CREATE TABLE
mgogala=# insert into procedures values('10');
INSERT 0 1
mgogala=# select runway from procedures where runway=10;
ERROR: operator does not exist: character varying = integer
LINE 1: select runway from procedures where runway=10;
^
HINT: No operator matches the given name and argument type(s). You might
need to add explicit type casts.
mgogala=# select runway from procedures where runway::int=10;
runway
--------
10
(1 row)



--
http://mgogala.byethost5.com

Reply With Quote
  #5  
Old   
Bjarne Jensen
 
Posts: n/a

Default Re: "explicit type casts" ? - 08-08-2011 , 04:13 AM



On 2011-08-07 19:37, Mladen Gogala wrote:

Quote:
mgogala=# select runway from procedures where runway::int=10;

So thats how it is used! Thanks a lot.

/ Bjarne

Reply With Quote
  #6  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: "explicit type casts" ? - 08-08-2011 , 07:03 AM



On Mon, 08 Aug 2011 11:13:12 +0200, Bjarne Jensen wrote:


Quote:
So thats how it is used! Thanks a lot.

/ Bjarne
This actually rather sloppy coding practice. If the column cannot be
converted to integer, the query will fail. I would advise enveloping the
cast in your own function which would return something expected, instead
of throwing an error.



--
http://mgogala.byethost5.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.