Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> How to compare VARCHAR2 and CHAR?

How to compare VARCHAR2 and CHAR?

From: <suisum_at_freenet.edmonton.ab.ca>
Date: 16 Oct 1998 17:49:48 GMT
Message-ID: <7080vs$n7u$1@news.sas.ab.ca>


A copy of this message sent to comp.oracle.misc

Hi Thomas:

I have trouble to compare the string values this time. I have the following codes to show my problem. I find that if I pass the string values to a function to do the comparision, it just can'ttell the NULL values. Would it be a problem becasue it is VARCHAR2 Vs. CHAR? Please help.

Best regards,
Still

PROCEDURE test2 IS

	i_char		intl_cmpny_cp.cmpny_shrt_nam%TYPE;
	i_varchar	internal_company_detail.short_name%TYPE;

   -- ======================================================
   FUNCTION is_equal_fn (
	i_value1		IN VARCHAR2,
	i_value2		IN VARCHAR2)
	RETURN BOOLEAN
   IS
	v_match	BOOLEAN := FALSE;
	v_varchar1	VARCHAR2(2000);
	v_varchar2	VARCHAR2(2000);

   BEGIN

	DBMS_OUTPUT.PUT_LINE ('i_value1: ' || i_value1 ||
		'i_value2: ' || i_value2);


-- test for perfectly match.
v_match := (i_value1 = i_value2); IF v_match THEN DBMS_OUTPUT.PUT_LINE ('v_match1 true'); ELSE DBMS_OUTPUT.PUT_LINE ('v_match1 false'); END IF;
-- If not match, test for NULL values.
IF NOT v_match THEN v_match := i_value1 IS NULL AND i_value2 IS NULL; END IF; IF v_match THEN DBMS_OUTPUT.PUT_LINE ('v_match2 true'); ELSE DBMS_OUTPUT.PUT_LINE ('v_match2 false'); END IF;
-- If still not match, trim the string and test it all over.
IF NOT v_match THEN v_match := (RTRIM(i_value1) = RTRIM(i_value2)); END IF; IF v_match THEN DBMS_OUTPUT.PUT_LINE ('v_match3 true'); ELSE DBMS_OUTPUT.PUT_LINE ('v_match3 false'); END IF;
-- If not match, test for NULL values.
IF NOT v_match THEN v_varchar1 := LTRIM(RTRIM(i_value1, ' '), ' '); v_varchar2 := LTRIM(RTRIM(i_value2, ' '), ' '); IF v_varchar1 IS NULL AND v_varchar2 IS NULL THEN v_match := TRUE; END IF; END IF; IF v_match THEN DBMS_OUTPUT.PUT_LINE ('v_match4 true'); ELSE DBMS_OUTPUT.PUT_LINE ('v_match4 false'); END IF;
-- If not match, test for NULL values.
IF NOT v_match THEN v_varchar1 := NVL(LTRIM(RTRIM(i_value1, ' '), ' '), '`'); v_varchar2 := NVL(LTRIM(RTRIM(i_value2, ' '), ' '), '`'); IF v_varchar1 = v_varchar2 THEN v_match := TRUE; END IF; END IF; IF v_match THEN DBMS_OUTPUT.PUT_LINE ('v_match5 true'); ELSE DBMS_OUTPUT.PUT_LINE ('v_match5 false'); END IF;
-- If not match, test for NULL values.
IF NOT v_match THEN IF NVL(RTRIM(i_value1, ' '), '/') = NVL(RTRIM(i_value2, ' '), '/') THEN v_match := TRUE; END IF; END IF; IF v_match THEN DBMS_OUTPUT.PUT_LINE ('v_match6 true'); ELSE DBMS_OUTPUT.PUT_LINE ('v_match6 false'); END IF; RETURN v_match;

   END is_equal_fn;

	i_char	  := 'ABCD';
	i_varchar := 'ABCD';
	IF is_equal_fn(i_char, i_varchar) THEN
	  DBMS_OUTPUT.PUT_LINE ('OK');
	END IF;	  

	i_char	  := 'ABCD     ';
	i_varchar := 'ABCD';
	IF is_equal_fn(i_char, i_varchar) THEN
	  DBMS_OUTPUT.PUT_LINE ('OK');
	END IF;	  

	i_char	  := '    ';
	i_varchar := ' ';
	IF is_equal_fn(i_char, i_varchar) THEN
	  DBMS_OUTPUT.PUT_LINE ('OK');
	END IF;	  

	i_char	  := '    ';
	i_varchar := NULL;
	IF is_equal_fn(i_char, i_varchar) THEN
	  DBMS_OUTPUT.PUT_LINE ('OK');
	END IF;	  

	i_char	  := NULL;
	i_varchar := NULL;
	IF i_char IS NULL AND i_varchar IS NULL THEN
	  DBMS_OUTPUT.PUT_LINE ('From mainline - ALL NULL');
	END IF;
	IF is_equal_fn(i_char, i_varchar) THEN
	  DBMS_OUTPUT.PUT_LINE ('OK');
	END IF;	  

END; Received on Fri Oct 16 1998 - 12:49:48 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US