I'm trying to create a version of InStr function for Informix 11.5. Here's the code:
CREATE FUNCTION MY_INSTR(txt VARCHAR(255), term VARCHAR(255))
RETURNING VARCHAR(255);
DEFINE i INTEGER;
DEFINE len INTEGER;
DEFINE lenterm INTEGER;
LET i = 1;
LET lenterm = LENGTH(term);
LET len = LENGTH(txt) - lenterm;
WHILE ( i <= len ) LOOP
IF SUBSTR( txt, i, lenterm ) = term THEN
RETURN i;
END IF
LET i = i + 1;
END LOOP
RETURN 0;
END FUNCTION;
I'll use this function to get the first name of a user in a column with the full name, like this:
SELECT SUBSTR(name, 1, MY_INSTR(name, " ")) AS firstname
FROM users
I need to pass the value " "
as the term, but when I do, lenterm
equals 0.
Is there a way to preserve white spaces when passing them to a function in Informix?
For
VARCHAR
types, theOCTET_LENGTH
function should return the length including trailing spaces. From the manual:I didn't have an informix 11.50 instance to test, but tried the following in an Informix 12.10 (FC6DE):
The result I got: