简体   繁体   中英

SQLite - Extract substring between delimiters for REPLACE function

I have a column field: location . I need to extract the string between the first and second delimeter ('/') .

I already have a column name where I ltrim to the first '/' . I've tried to create a similar query with a combination of rtrim , replace , substr as my source column to no avail. Here is what my data looks like. I want to extract AML , for example. Right now, there are only three options ( value1 , value2 , value3 ) between the first and second delimiters, but there could be more later.

Attribute data
----------+--------------------------------------------------------------------------------------------------------------------
 Field    | First value
----------+--------------------------------------------------------------------------------------------------------------------
 location | './AML/Counties/*****************kyaml_20190416_transparent_mosaic_group1.tif'
 name     | 'kyaml_20190416_transparent_mosaic_group1.tif'
----------+--------------------------------------------------------------------------------------------------------------------

What is the best way of creating my column source with the value from location ?

Output should be like this:

Attribute data
----------+--------------------------------------------------------------------------------------------------------------------
 Field    | First value
----------+--------------------------------------------------------------------------------------------------------------------
 location | './AML/Counties/****************kyaml_20190416_transparent_mosaic_group1.tif'
 name     | 'kyaml_20190416_transparent_mosaic_group1.tif'
 source   | 'AML'
----------+--------------------------------------------------------------------------------------------------------------------

With substr() and instr() :

select *,
  substr(
    substr(location, instr(location, '/') + 1),
    1,  
    instr(substr(location, instr(location, '/') + 1), '/') - 1
  ) as source
from data

See the demo .

I used forpas query to modify my query. Here is my final query

ogrinfo box_tiles.shp -dialect SQLITE -sql \
 "UPDATE box_tiles SET source = \
  substr(\
    substr(location, instr(location, '/') + 1), 1, \
    instr(substr(location, instr(location, '/') + 1), '/') - 1)"

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