Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Feeding SELECT output to LIKE

Problem:

select STR1 from T1 where STR2 = 'NAME1'

In the above query STR1 can be in form {ABC, ABC_1, ABC_2,..., MNO, XYZ, XYZ_1...}.

So let suppose I have following output

ABC_1
MNO
XYZ

Now I want to extract all those matching STR1 that include the part before _#. For example the expected output for the example dataset above is:

ABC
ABC_1
ABC_2

MNO

XYZ
XYZ_1

Note that STR2 is always unique per STR1.

Code wise I imagine some thing like following:

SELECT 
    STR1 
FROM 
    T1 
WHERE
    STR1 
LIKE '% (truncate_underscore_part(select STR1 from T1 where STR2 = 'NAME1')) %'

Any idea?

First solution:

select t1.str1
  from (
  select case when instr( str1, '_' ) > 0
                then substr( str1, 1, instr( str1, '_' ) - 1 )
              else str1
         end prefix
    from t1 where str2 = 'NAME1'
) prefix_list,
  t1
  where t1.str1 like prefix || '%'
like image 511
LovelyVirus Avatar asked Mar 21 '12 15:03

LovelyVirus


People also ask

How do you use output in a query?

You can use OUTPUT in applications that use tables as queues, or to hold intermediate result sets. That is, the application is constantly adding or removing rows from the table. The following example uses the OUTPUT clause in a DELETE statement to return the deleted row to the calling application. SQL.

How to use the output clause in a SELECT statement?

You can't use the output clause in a select statement. It's only applicable in insert, update, delete and merge. Then, insert the records into the temporary table using insert...output...select: INSERT INTO #Jedi OUTPUT INSERTED.*

What is an example of select into in SQL?

SQL SELECT INTO Examples. The following SQL statement creates a backup copy of Customers: SELECT * INTO CustomersBackup2017. FROM Customers; The following SQL statement uses the IN clause to copy the table into a new table in another database: SELECT * INTO CustomersBackup2017 IN 'Backup.mdb'. FROM Customers;

What is output_table in SQL Server?

output_table. Specifies a table that the returned rows are inserted into instead of being returned to the caller. output_table may be a temporary table.


1 Answers

with prefix_list as (
  select regexp_substr( str1, '^[A-Z]*' ) prefix from t1 where str2 = 'NAME1'
)
select t1.str1 from t1 join prefix_list
        on t1.str1 = prefix_list.prefix
           or regexp_like( t1.str1, prefix_list.prefix||'_[0-9]' )

To do it without the regexp functions (for older Oracle versions), it depends a bit on how much you want to validate the format of the strings.

select t1.str1
  from (
  select case when instr( str1, '_' ) > 0
                then substr( str1, 1, instr( str1, '_' ) - 1 )
              else str1
         end prefix
    from t1 where str2 = 'NAME1'
) prefix_list,
  t1
where t1.str1 = prefix
   or t2.str1 like prefix || '\__' escape '\'
like image 73
Dave Costa Avatar answered Oct 29 '22 16:10

Dave Costa