简体   繁体   中英

How to SELECT a field without the first n characters and the last m characters?

I have a SELECT statement that is trying to view data from a single table. The data is encapsulated by HTML tags ( <p> and </p> ). For instance, one field might say:

<p>Lorem Ipsum</p>

How do I tell the SELECT statement to discount the first 3 characters and the last 4 characters?

SELECT SUBSTRING(data, 4, LENGTH(data)-7)

If all data has only one opening tag at the beginning, and one closing tag at the end, like in your example, you could use this:

select
  case when instr(data, '</')>instr(data, '>') then
    substring(data, instr(data, '>')+1,instr(data, '</')-instr(data, '>')-1)
  else
    data
  end as stripped_data
from
  your_table

notice that this will strip also unmatched tags like <p>Lorem Ipsum</strong>

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