instr function
Posted in 2003
Topics: General Discussion
Has anyone created an instr type of function for use in informix ids? Here is the format of the function: instr instr(string1,string2[,start-position]) - finds the position of string2 in string1. The parentheses around the start-position attribute denot that it is optional. Example: instr(ename,'S') Finds the position of the character S in values of the ENAME attribute thanks, brian
I don't think there is a builtin function for this. However you can easily write a function instr for this. What version of IDS are you using? Ravi. ----- Original Message ----- From: "BRIAN HOUSE" <houseb@intelihealth.com> To: <ids@iiug.org> Sent: Tuesday, June 17, 2003 14:41 Subject: instr function [1379] > Has anyone created an instr type of function for use in informix ids? > > Here is the format of the function: > > instr > instr(string1,string2[,start-position]) - finds the position of string2 in string1. The parentheses around the start-position attribute denot that it is optional. > > Example: instr(ename,'S') > > Finds the position of the character S in values of the ENAME attribute > > > thanks, > brian > >
Here you go.
Create this function:-
create function instr(p_string lvarchar,p_token lvarchar,p_start integer
default 1 )
returning smallint ;
define i,j smallint ;
define w_1 lvarchar ;
if ( p_string is NULL) or (p_token is NULL ) then
return -1 ;
end if ;
let j = length(p_string);
for i = p_start to j
if ( substr(p_string,i,1) = substr(p_token,1,1) ) then
let w_1 = substr(p_string,i,length(p_token)) ;
if ( w_1 = p_token) then
return i ;
end if ;
end if ;
end for ;
return 0 ;
end function ;
A: execute function instr("RAVI KRISHNA","K")
will return 6;
B: execute function instr("RAVI KRISHNA","K",4);
will return 6 ;
The third parameter is optional. The return value will still be same,
however it will be faster if the starting index is given as the
third parameter.
C: execute function instr("RAVI KRISHNA","RIS")
will return 7;
The second argument can either be a single character or a string.
Examples A and B searched for a single char. This one searches for
a string.
If a null is sent to one of the two parameters, it will return -1.
if a string is not found, it will return 0 ;
----- Original Message -----
From: "House, Brian" <HouseB@intelihealth.com>
To: "rkusenet" <rkusenet@sympatico.ca>; <ids@iiug.org>
Sent: Tuesday, June 17, 2003 15:05
Subject: RE: instr function [1379]
We are using ids 9.30.UC1.
thanks
-----Original Message-----
From: rkusenet [mailto:rkusenet@sympatico.ca]
Sent: Tuesday, June 17, 2003 3:02 PM
To: ids@iiug.org; House, Brian
Subject: Re: instr function [1379]
I don't think there is a builtin function for this.
However you can easily write a function instr for this.
What version of IDS are you using?
Ravi.
----- Original Message -----
From: "BRIAN HOUSE" <houseb@intelihealth.com>
To: <ids@iiug.org>
Sent: Tuesday, June 17, 2003 14:41
Subject: instr function [1379]
> Has anyone created an instr type of function for use in informix ids?
>
> Here is the format of the function:
>
> instr
> instr(string1,string2[,start-position]) - finds the position of
> string2 in string1. The
parentheses around the start-position attribute denot that it is
optional.
>
> Example: instr(ename,'S')
>
> Finds the position of the character "S" in values of the ENAME
> attribute
>
>
> thanks,
> brian
>
>