redshift rtrim and collation

0

In redshift, how do I use rtrim to get the original string with its original casing, but match the string to remove case insenstive?

These aren't acceptable solutions:

rtrim('aBcd', 'Cd') = aBc
rtrim('aBcd', 'cd') = aB
rtrim(lower('aBcd'), 'cd) = ab

And this doesn't work

rtrim(collate('aBcd', 'case_insensitive), collate('Cd', 'case_insensitive')) != aB

Do I have to use substring instead?

질문됨 일 년 전241회 조회
2개 답변
1

The solution is to use a combination of upper, position, substring, and case.

create temporary table mytable (mystring varchar(100));
insert into mytable values ('aBcd'), ('aBCd'), ('aBCD'), ('aBcD');
select substring(mystring, 1, position('CD' in upper(mystring)) - 1) 
from mytable;
 substring 
-----------
 aB
 aB
 aB
 aB

If your data has some rows without some form of CD at the end of it, you need to add some additional logic.

insert into mytable values ('xy'), ('Xy'), ('xY'), ('XY');
select substring(mystring, 1, case when position('CD' in upper(mystring)) = 0 then length(mystring) else position('CD' in upper(mystring)) - 1 end) 
from mytable;
profile pictureAWS
전문가
답변함 일 년 전
0

Hi,

I would use the regexp_substr function, it provides several options with less code https://docs.aws.amazon.com/redshift/latest/dg/REGEXP_SUBSTR.html.

Example: select regexp_substr('aBcd','aB');

Regards

AWS
전문가
Ziad
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠