473,769 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obtain boolean value of expression in PLPGSQL

Hello

My task is to validate expression and get value of it in boolean variable.
I try to do following:
CREATE OR REPLACE FUNCTION get_value(integ er) RETURNS boolean AS'
DECLARE
Ret boolean;
Op TEXT = ''='';
BEGIN

Ret := 3 || Op || $1;
RETURN Ret;
END;
' language 'plpgsql';

when I try to execute this function I get such result:

bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'
bill=#

So, how can I explain that resulting type must be boolean ? Also, I try to
play with SELECT INTO with same result.

--
.... All opinions expressed are mine and not those of my employer.

Yours, Max [Msg N 2419]
-------------------------------------------
mailto: mx@lucky.net phone: +380-44-2054455

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

Nov 12 '05 #1
3 4403
On Tuesday 04 November 2003 10:06, Max Speransky wrote:
Hello

My task is to validate expression and get value of it in boolean variable.
I try to do following: Ret := 3 || Op || $1;
RETURN Ret; bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'


The problem is that your expression is being evaluated as a string. In the
absence of an eval() function, you probably should do something like:

my_stmt := ''SELECT '' || 3 || Op || $1;
EXECUTE my_stmt;
FOR EACH ...

So - build a simple query, execute it and read the result. Should do what you
want.

--
Richard Huxton
Archonet Ltd

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

Nov 12 '05 #2
Hello

You are on wrong way.

Try like

DECLARE
b RECORD;
s TEXT;
op TEXT;
BEGIN op := ''='';
s := ''SELECT 3 ''|| op || ''|| $1 || ''AS b'';
FOR b IN EXECUTE s LOOP
RETURN b.b;
END LOOP;
END;

Regards
Pavel
On Tue, 4 Nov 2003, Max Speransky wrote:
Hello

My task is to validate expression and get value of it in boolean variable.
I try to do following:
CREATE OR REPLACE FUNCTION get_value(integ er) RETURNS boolean AS'
DECLARE
Ret boolean;
Op TEXT = ''='';
BEGIN

Ret := 3 || Op || $1;
RETURN Ret;
END;
' language 'plpgsql';

when I try to execute this function I get such result:

bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'
bill=#

So, how can I explain that resulting type must be boolean ? Also, I try to
play with SELECT INTO with same result.

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

Nov 12 '05 #3
Max Speransky said:
Hello

My task is to validate expression and get value of it in boolean variable. [snip] when I try to execute this function I get such result:

bill=# select get_value(3);
WARNING: Error occurred while executing PL/pgSQL function get_value
WARNING: line 6 at assignment
ERROR: Bad boolean external representation '3=3'


|| is the string concatenation operator - so your expression creates a
string "3=3" and then tries to return that as a boolean hence the error.

if you cannot just do "return 3 = $1;" perhaps you could try using EXECUTE?

cheers
Martin
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2526
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators ======================================== SUMMARY This PEP proposes an extension to permit objects to define their own
0
1349
by: Frank van Vugt | last post by:
Hi, FWIW, I walked into this one when changing an int into a numeric(5,1) : IF ( 0::int ) => interpreted as false IF ( 0.0::numeric(5,1) ) => interpreted as true
1
3886
by: Philippe Lang | last post by:
Hello, Imagine we have the following kind of table, with two values (a and b), and a varchar (f) representing an expression. ---------------------------------- CREATE TABLE public.test ( id serial NOT NULL, a int4, b int4,
6
2950
by: anonymous | last post by:
hi all, I'm trying to use the capCreateCaptureWindow (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_capcreatecapturewindow.asp) in a Windows Application Project. I want to pass Image as one of the parameter but I figure that an Image doesn't have a handle. Previously I had it working in a Web Application Project using the handle of a PictureBox. Is there any workaround? Please help and...
9
4157
by: HS1 | last post by:
Hello Could you please help for a simple boolean expression If a is not equal to Null I tried If (a != null) Then
16
3253
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
6
1667
by: GHUM | last post by:
I need to split a text at every ; (Semikolon), but not at semikolons which are "escaped" within a pair of $$ or $_$ signs. My guess was that something along this should happen withing csv.py; but ... it is done within _csv.c :( Example: the SQL text should be splitted at "<split here>" (of course, those "split heres" are not there yet :) set interval 2;
7
7358
by: rguarnieri | last post by:
Hi! I'm trying to create a query with a boolean expression like this: select (4 and 1) as Value from Table1 this query return always -1, but when I make the same calculation in visual basic, the value returned is 0. Can anyone tell me why the expression (4 and 1) return different value
4
306
by: Greg Corradini | last post by:
Hello all, I'm having trouble understanding why the following code evaluates as it does: True -1 In the 2.4 Python Reference Manual, I get the following explanation for the 'and' operator in 5.10 Boolean operations: " The expression x and y first evaluates x; if x is false, its value is
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9848
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7392
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.