Search code examples
sqloracle-databaseplsql

Export some part of barcode string GS1-128


On package box I have barcode (01)18777777777777(15)241129(10)L3333333(90)02 after scan with devices get 01187777777777771524112910L3333333F9002

In database I have small table barcode_settings

CODE NAME SIZE
01 GTIN 14
10 LOT -1 is variable length end is character F
15 Best Before Date (YYMMDD) 6
90 Total 2

Is it possible get some like this:

my_func with two parameters P_STRING and P_CODE

select my_func('01187777777777771524112910L3333333F9002','01') from dual; 18777777777777

select my_func('01187777777777771524112910L3333333F9002','10') from dual; L3333333

select my_func('01187777777777771524112910L3333333F9002','15') from dual; 241129


Solution

  • That's possible, but with some restrictions, e.g.

    • barcode has to always have the same format because - if it doesn't - there's no way to distinguish which e.g. 01 is which. Your original (before you changed it) sample barcode illustrates that:

      01180120708010531524112910L2238415F9002
      --  --
      two 01 values; which of them is supposed to represent CODE value?
      
    • codes have to follow one after exactly as in that example: 01, 15, 10, 90; otherwise, you can't properly calculate lengths


    SQL> create or replace function f_bc(par_string in varchar2, par_code in varchar2)
      2  return varchar2 is
      3    l_len_01 number;
      4    l_len_10 number;
      5    l_len_15 number;
      6    l_len_90 number;
      7    --
      8    l_01 varchar2(20);
      9    l_10 varchar2(20);
     10    l_15 varchar2(20);
     11    l_90 varchar2(20);
     12    retval varchar2(20);
     13  begin
     14    select csize into l_len_01 from barcode_settings where code = '01';
     15    select csize into l_len_10 from barcode_settings where code = '10';
     16    select csize into l_len_15 from barcode_settings where code = '15';
     17    select csize into l_len_90 from barcode_settings where code = '90';
     18
     19    l_01 := substr(par_string, 3, l_len_01);
     20    l_15 := substr(par_string, 2 + l_len_01 + 2 + 1, l_len_15);
     21    l_10 := substr(par_string,
     22                   2 + l_len_01 + 2 + l_len_15 + 2 + 1,
     23                   length(par_string) - (2 + l_len_01 + 2 + l_len_15 + 2 + 1 + 2 + l_len_90));
     24    l_90 := substr(par_string, -2);
     25
     26    retval := case when par_code = '01' then l_01
     27                   when par_code = '15' then l_15
     28                   when par_Code = '10' then l_10
     29                   when par_code = '90' then l_90
     30              end;
     31    return retval;
     32  end;
     33  /
    
    Function created.
    

    Testing:

    SQL> with test (barcode) as
      2    (select '01180120708010531524112910L2238415F9002' from dual)
      3  select f_bc(barcode, '01') v_01,
      4         f_bc(barcode, '15') v_15,
      5         f_bc(barcode, '10') v_10,
      6         f_bc(barcode, '90') v_90
      7  from test;
    
    V_01            V_15   V_10       V_90
    --------------- ------ ---------- -----
    18012070801053  241129 L2238415   02
    
    SQL>