Search code examples
sqloracleinner-joinsql-like

Oracle SQL select final value of one table column like value from other table column that contains value minus 1 digit


At an Oracle Database, I have 1 DB table1 column which contains a known value1, for example: 4649843545945894537 And there is another 2nd DB table at this database that contains a column with the value of the 1st DB table plus 1 random digit, which is the complete value (value2) for this number and it is unique. For example complete value2 of the 2nd table is: 46498435459458945374 So, using SQL I need to find the complete value2 of the number at the 2nd table by selecting the known value1 of the 1st table and using "like" to find the complete the unique value2 at the 2nd table. So, need SQL to be something like (pseudo code/SQL):

select value2 from table2 where value2 like '$(value1)%';

So, at our example case, should be translated to the following:

select value2 from table2 where value2 like '4649843545945894537%';

which should return for value2 the 46498435459458945374 Hope this is clear what I need. Thank you.


Solution

  • You seem to want a join:

    select t1.value1, t2.value2
    from t1
    inner join t2 on t2.value2 like t1.value1 || '_'
    

    This allows just one extra character at the end of value2.

    The query assumes that your values are strings. If you have numbers instead, then I would recommend arithmetics:

    select t1.value1, t2.value2
    from t1
    inner join t2 on floor(t2.value2/10) = t1.value1