Search code examples
firebird

How to extract letters from a string using Firebird SQL


I want to implement a stored procedure that extract letters from a varchar in firebird.

Example :

v_accountno' is of type varchar(50) and has the following values 
accountno 1 - 000023208821 
accountno 2 - 390026826850868140H
accountno 3 - 0700765001003267KAH

I want to extract the letters from v_accountno and output it in o_letter. In my example: o_letter will store H for accountno 2 and KAH for accountno 3.

I tried the following stored procedure, which obviously won't work for accountno 3. (Please help).

  CREATE OR ALTER PROCEDURE SP_EXTRACT_LETTER
  returns (
      o_letter varchar(50))
  as
  declare variable v_accountno varchar(50);
  begin
       v_accountno = '390026826850868140H';

       if (not (:v_accountno similar to '[[:DIGIT:]]*')) then
       begin
           -- My SP won't work in for accountno 3 '0700765001003267KAH'
           v_accountno = longsubstr(v_accountno, strlen(v_accountno), strlen(v_accountno));
           o_letter = v_accountno;
       end

      suspend;
  end

Solution

  • One solution would be to replace every digits with empty string like:

    o_letter  = REPLACE(v_accountno, '0', '')
    o_letter  = REPLACE(o_letter, '1', '')
    o_letter  = REPLACE(o_letter, '2', '')
    ...