You Asked
is there just ONE space between words or is it totally free form? Actually, it's a totally free form. Thanks for your response.
Hi Tom,
I have a sql statement:
Select Document_title ||'or'|| Document_title
from atas_query_v
I understand this just repeats the column twice using 'OR' in between. What I'm looking for is a way to have the 'OR' placed between each word for that column. Is that possible to accomplish in a SQL output? I know this sounds like a strange request, but I need to use the 'OR' between each keyword in my Oracle Forms application. Users have one text item which queries the DB using the Intermedia text function. If they enter two keywords it searches on the exact keyword. I want to use the OR function between keywords so more results are returned. I hope this makes sense...
Thanks in advance,
Randy
and Tom said...
We'll have to write a stored procedure to do that. Here is the code I use on asktom to process the search string you guys type in:
create or replace package delimited
as
function word( p_str in varchar2,
p_n in varchar2,
p_enclosed_by in varchar2 default '''',
p_separated_by in varchar2 default ',' )
return varchar2;
function context_query( p_search_str in varchar2,
p_sep in varchar2 default 'OR' )
return varchar2;
pragma restrict_references( word, WNDS, RNDS );
end;
/
create or replace package body delimited
as
type vcArray is table of varchar2(2000) index by binary_integer;
g_words vcArray;
g_empty vcArray;
g_last_string varchar2(4096);
function de_quote( p_str in varchar2,
p_enc_by in varchar2 )
return varchar2
is
begin
if ( p_enc_by is null )
then
return p_str;
else
return replace( ltrim( rtrim( p_str, p_enc_by ), p_enc_by ),
p_enc_by||p_enc_by, p_enc_by );
end if;
end de_quote;
procedure parse( p_str in varchar2,
p_delim in varchar2,
p_sep in varchar2 )
is
l_n number default 1;
l_in_quote boolean default FALSE;
l_ch char(1);
l_len number default nvl(length( p_str ),0);
begin
if ( l_len = 0 ) then
return;
end if;
g_words := g_empty;
g_words(1) := NULL;
for i in 1 .. l_len loop
l_ch := substr( p_str, i, 1 );
if ( l_ch = p_delim ) then
l_in_quote := NOT l_in_quote;
end if;
if ( l_ch = p_sep AND NOT l_in_quote ) then
l_n := l_n + 1;
g_words(l_n) := NULL;
else
g_words(l_n) := g_words(l_n)||l_ch;
end if;
end loop;
for i in 1 .. l_n loop
g_words(i) := de_quote( g_words(i), p_delim );
end loop;
end parse;
function word( p_str in varchar2,
p_n in varchar2,
p_enclosed_by in varchar2 default '''',
p_separated_by in varchar2 default ',' )
return varchar2
is
begin
if ( g_last_string is NULL or p_str <> g_last_string ) then
g_last_string := p_str;
parse( p_str, p_enclosed_by, p_separated_by );
end if;
return g_words( p_n );
exception
when no_data_found then return NULL;
end;
function context_query( p_search_str in varchar2,
p_sep in varchar2 default 'OR' )
return varchar2
is
l_temp_value varchar2(1000);
l_return_value varchar2(1000) := NULL;
l_start_token number := 1;
l_in_token boolean := FALSE;
l_sep varchar2(20);
begin
if nvl(length(p_search_str),0) = 0 then
return NULL;
end if;
--
l_temp_value := trim(replace( lower(p_search_str), ':', ' '));
l_temp_value := trim(replace( l_temp_value, ';', ' '));
l_temp_value := trim(replace( l_temp_value, '"', ' '));
l_temp_value := trim(replace( l_temp_value, ':'',', ' '));
l_temp_value := trim(replace( l_temp_value, '(', ' '));
l_temp_value := trim(replace( l_temp_value, ')', ' '));
l_temp_value := trim(replace( l_temp_value, '!', ' '));
l_temp_value := trim(replace( l_temp_value, '&', ' '));
l_temp_value := trim(replace( l_temp_value, '+', ' '));
l_temp_value := trim(replace( l_temp_value, '-', ' '));
l_temp_value := trim(replace( l_temp_value, ',', ' '));
l_temp_value := trim(replace( l_temp_value, ' and ', ' ' ));
l_temp_value := trim(replace( l_temp_value, ' or ', ' ' ));
--
if length(l_temp_value) > 0 then
l_in_token := TRUE;
end if;
--
for i in 1..length(l_temp_value) loop
if substr(l_temp_value,i,1) = ' ' then
if l_in_token = TRUE then
l_return_value := l_return_value || l_sep || ' {' ||
substr(l_temp_value,l_start_token, i-l_start_token) ||
'}' || ' ';
l_sep := p_sep;
l_in_token := FALSE;
end if;
elsif l_in_token = FALSE then
l_in_token := TRUE;
l_start_token := i;
end if;
end loop;
if l_in_token = TRUE then
l_return_value := l_return_value || l_sep || ' {' ||
substr(l_temp_value,l_start_token) || '} ';
end if;
return trim(l_return_value);
end;
end delimited;
/
You use it like this:
ops$tkyte@ORA817DEV.US.ORACLE.COM> select delimited.context_query( 'how now brown cow' ) from dual;
DELIMITED.CONTEXT_QUERY('HOWNOWBROWNCOW')
-----------------------------------------------------------------------------------------------------------------------------------
{how} OR {now} OR {brown} OR {cow}
ops$tkyte@ORA817DEV.US.ORACLE.COM> select delimited.context_query( 'how now brown cow', 'AND' ) from dual;
DELIMITED.CONTEXT_QUERY('HOWNOWBROWNCOW','AND')
-----------------------------------------------------------------------------------------------------------------------------------
{how} AND {now} AND {brown} AND {cow}
Rating
(7 ratings)
Is this answer out of date? If it is, please let us know via a Comment