简体   繁体   中英

Sqlite - How to remove substring in a field using wildcards

I would like to remove all occurrences of a certain string pattern across all fields in a table.

For example, find all occurrences of the pattern "<XY>*<Xy>" where * represents any possible configuration of characters. I want to remove just those substrings and leave the remainder of the string intact.

This is an example of what I would like to use as my SQL command, but of course this doesn't work:

UPDATE Table SET Field = replace(Field, '<XY>*<Xy>', '');

What is the solution?

Here is an option which attempts to splice around the <XY>...</XY> tags:

UPDATE yourTable
SET Field = SUBSTR(Field, 1, INSTR(Field, '<XY>') - 1) ||
            SUBSTR(Field, INSTR(Field, '</XY>') + 5)
WHERE Field LIKE '%<XY>%</XY>%'

It updates fields containing this pattern with the concatenation of everything coming before the first <XY> and everything coming after the second </XY> .

Note that I used <XY>...</XY> rather than what you had originally, because INSTR() is not case sensitive, and both tags would appear as being the same thing.

Demo

The demo is for MySQL but the sytnax is almost identical to SQLite.

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