SQL を使用して 2 つ以上の文字列の共有開始を特定するにはどうすればよいですか?
たとえば、3 つの URI があるとします。
'http://www.example.com/service/1'
'http://www.example.com/service/2'
'http://www.example.com/service/3'
'http:://www.example.com/service/'
SQL を使用してすべての共有を決定するにはどうすればよいですか?
SQL を使用して 2 つ以上の文字列の共有開始を特定するにはどうすればよいですか?
たとえば、3 つの URI があるとします。
'http://www.example.com/service/1'
'http://www.example.com/service/2'
'http://www.example.com/service/3'
'http:://www.example.com/service/'
SQL を使用してすべての共有を決定するにはどうすればよいですか?
create or replace function string_common_start (s text, t text)
returns text as $$
declare
sa char(1)[] = string_to_array(s, null);
ta char(1)[] = string_to_array(t, null);
output_s text = '';
begin
if s = t or t = '' or s = ''
then return t;
end if;
for i in 1 .. least(length(s), length(t)) loop
if sa[i] = ta[i]
then
output_s = output_s || sa[i];
else
exit;
end if;
end loop;
return output_s;
end; $$ language plpgsql strict;
create aggregate string_common_start (text) (
sfunc = string_common_start,
stype = text
);
null 値を集計しません
select string_common_start(s)
from (values
('http://www.example.com/service/1'),
('http://www.example.com/service/2'),
('http://www.example.com/service/3'),
(null)
) s(s);
string_common_start
---------------------------------
http://www.example.com/service/