简体   繁体   中英

If string contains any value from another column, return that value - SQL Informix

I have one column that has very lengthy string, and somewhere (not in a precise location) is a 3 digit number that I want to extract. That part of a string, can be anything from a defined list of another one-column table.

So, if part of the string contains a defined value from a column, return that value. How can I do this with Informix SQL?

Thanks!

This is a string from which I want to extract info:

Insert nalog14: 8880888802981130 GERARD BUTLER  KLEVER STR.37 null XANTEN GERMANY null null null 0045000000000 COMERZ AGILE AD null 02085020 01 null Wed Feb 05 00:00:00 CET 2020 978 null 6496.14 NL69ABNA0494540044 Brandsma Yachtservice Eeltjebaasweg 6-8 Sneek THE NETHERLAND 00 528 A 528 null ABNANL2AXXX DOMESTICUSAMSTERDAM This is some random text 130 Invoice number 112362 Repair of Brandsma Vlet Winter Storage Brandsma Vlet null S null 0 null 000 0 null 0 null 1 GERADS SE

And let's say that this is my referent column:

220
150
400
300
130
112
...

So my string contains value '130' there after 'This is some random text' part of the string, and that values is also in referent column.

My desired output is just to extract that value from a string.

130

What is also problematic is that part of the string that I want to extract can also be present somewhere else in the string, as you can see at the beginning 8880888802981 130 , but this occurred by chance. Only pattern that I can recognize is that desired output is always separated with blank space before and after.

I hope this helps.

table1 :

line
------
"Insert nalog14: 8880888802981130 GERARD..."

table2 :

number
------
"220"
"150"
"400"

...

select number
from table1,table2
where line like "%"||trim(number)||"%"

I'm assuming that there is a table Table1 with:

CREATE TABLE Table1 ( …, line LVARCHAR, … );
CREATE TABLE Table2 (number CHAR(3) NOT NULL);

I'm also assuming that all the numbers in Table2 are 3-digit numbers.

You could use the INSTR (in-string) function:

 SELECT t2.number
   FROM Table1 AS t1
   JOIN Table2 AS t2 ON INSTR(t1.line, " " || t2.number || " ") != 0

where the values to be searched for are all 3-digit numbers (or 3-character strings, at any rate). If you are dealing with variable length data and the type in Table2 is not a variable-length type, then you need to apply TRIM to t2.number . It would be nice to avoid concatenation operations. If you can modify Table2 so that number CHAR(5) NOT ULL and the numbers are inserted with a leading space (there'll be a trailing space after the three-digit numbers automatically), then you can use

 SELECT TRIM(t2.number)
   FROM Table1 AS t1
JOIN Table2 AS t2 ON INSTR(t1.line, t2.number) != 0

If the numbers are variable length, this won't work — the concatenation is necessary, and you need to trim the values before using them in the comparison:

 SELECT t2.number
   FROM Table1 AS t1
   JOIN Table2 AS t2 ON INSTR(t1.line, " " || TRIM(t2.number) || " ") != 0

As noted in the answer by Antonio J. Ortiz , you could also use the LIKE operator instead of the INSTR function, or the MATCHES operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM