Home » SQL & PL/SQL » SQL & PL/SQL » About Boolean Function
About Boolean Function [message #199079] Fri, 20 October 2006 01:01 Go to next message
romi
Messages: 67
Registered: October 2006
Member
Hello Friends,

I am making a function to check that a word is palindrom or not.If it is plaindrom that it returns 1 otherwise 0.but i want to return boolean value "true" or "false".
My function is:-
Create or replace function palindrom(name in varchar2)return number
is
n number;
v_name varchar2;
t_name varchar2;
begin
n:=length(name);
for i in 1..n loop
v_name:=substr(name,1,n)
t_name:=t_name||substr(name,n-i+1,1);
end loop;
if v_name<>t_name then
return 0;
else
return 1;
end if;
end;

So,what changes should i do to achieve "TRUE" or "FALSE" return values instead of 0 and 1.
Re: About Boolean Function [message #199085 is a reply to message #199079] Fri, 20 October 2006 01:30 Go to previous messageGo to next message
sukhadukkham
Messages: 3
Registered: September 2006
Location: Switzerland
Junior Member
just change your function to this:
CREATE OR REPLACE FUNCTION palindrom ( NAME IN VARCHAR2
                                     )
                                     RETURN BOOLEAN
IS
  n NUMBER;
  v_name VARCHAR2(100);
  t_name VARCHAR2(100);
BEGIN
  n:=LENGTH(NAME);
  FOR
    i IN 1..n 
  LOOP
    v_name:=SUBSTR(NAME,1,n);
    t_name:=t_name||SUBSTR(NAME,n-i+1,1);
  END LOOP;
  IF
    v_name<>t_name 
  THEN
    RETURN FALSE;
  ELSE
  RETURN TRUE;
END IF;
END;

[Updated on: Fri, 20 October 2006 01:31] by Moderator

Report message to a moderator

Re: About Boolean Function [message #199086 is a reply to message #199085] Fri, 20 October 2006 01:31 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Next time, put your code in the appropriate tags. Thank you.

MHE
Re: About Boolean Function [message #199087 is a reply to message #199086] Fri, 20 October 2006 01:39 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Quote:
IF
v_name<>t_name
THEN
RETURN FALSE;
ELSE
RETURN TRUE;
END IF;


As a tutor of mine once said: "this shows you don't understand booleans. The way this should be coded is : return v_name = t_name;"

... and I still agree with him.

[Updated on: Fri, 20 October 2006 01:41]

Report message to a moderator

Re: About Boolean Function [message #199088 is a reply to message #199085] Fri, 20 October 2006 01:44 Go to previous messageGo to next message
romi
Messages: 67
Registered: October 2006
Member


Thanks,

I had done it but after this how can i execute it.

Plz help me.
Re: About Boolean Function [message #199089 is a reply to message #199088] Fri, 20 October 2006 01:56 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
plz is not a member here so I doubt whether he (she?) can help you.

here's a small test script:
CREATE FUNCTION is_palindrome(p_string IN VARCHAR2)
RETURN BOOLEAN
IS
   v_string VARCHAR2(100);
BEGIN
  -- cheating by using the undocumented
  -- "reverse" SQL function
  SELECT REVERSE(p_string)
  INTO   v_string
  FROM   dual;
  
  -- Applying Frank's advice
  RETURN p_string = v_string;
END is_palindrome;
/
sho err

set serverout on

var a VARCHAR2(100)

exec :a := 'civic';

--
-- Test it in PL/SQL: boolean functions cannot
-- be used in SQL
--
Begin
  if is_palindrome(:a)
  then
    dbms_output.put_line(:a||' is a palindrome ');
  else
    dbms_output.put_line(:a||' is not a palindrome ');  
  end if;  
End;
/

exec :a := 'another';
/

DROP function is_palindrome
/


copy/paste in a text editor, save as a ".SQL file" and run it in SQL*plus (@_the_path_of_the_file\yourfile.sql)

MHE
Re: About Boolean Function [message #199113 is a reply to message #199079] Fri, 20 October 2006 04:48 Go to previous message
romi
Messages: 67
Registered: October 2006
Member
Thanks to all
Previous Topic: Create a table from another table with all constraints
Next Topic: Run procedures in parallel (without duplication)
Goto Forum:
  


Current Time: Mon Jun 23 04:34:36 CDT 2025