假設字符串中只由'A'、'B'、'C'、'D'組成,且長度為7。並設函數REPLICATE(<字符串>,<n>)可以創建一個<字符串>的n個副本的字符串,另外還有REPLACE(<目標字符串>,<老字符串>,<新字符串>),實現如下:
1 BEGIN 2 DECLARE inString CHAR(7); 3 SET inString = 'DCDBAAB'; 4 5 REPLICATE('A', (DATA_LENGTH(inString)-DATA_LENGTH(REPLACE(inString,'A','')))) 6 || 7 REPLICATE('B', (DATA_LENGTH(inString)-DATA_LENGTH(REPLACE(inString,'B','')))) 8 || 9 REPLICATE('C', (DATA_LENGTH(inString)-DATA_LENGTH(REPLACE(inString,'C','')))) 10 || 11 REPLICATE('D', (DATA_LENGTH(inString)-DATA_LENGTH(REPLACE(inString,'D','')))) 12 13 END;