sqlsqlitesql-updatecollate

Sqlite: multiple update (find and replace) case insensitive


I use DB Browser for SQLite to visualize and update an sqlite file.

I am able to do run a case sensitive query to update some text like this:

UPDATE itemNotes  SET note = REPLACE(note ,  'sometext', 'abc');

But I would like to match replace all case combinations of sometext (e.g. sometext, SOMEtext, SOmeText...)

I tried to do this :

UPDATE itemNotes  SET note = REPLACE(LOWER(note),  'sometext', 'abc');

But this transform the whole content of the field note in lower case which isn't what I want.

I also tried these query but without success:

UPDATE itemNotes  SET note = REPLACE(note, BINARY 'sometext', 'abc')
UPDATE itemNotes  SET note = REPLACE(note, COLLATE Latin1_General_CS_AS'sometext', 'abc')

I am doing it on the zotero.sqlite, which is created by this file (line 85). The table is created by this query

CREATE TABLE itemNotes (
    itemID INTEGER PRIMARY KEY,
    parentItemID INT,
    note TEXT,
    title TEXT,
    FOREIGN KEY (itemID) REFERENCES items(itemID) ON DELETE CASCADE,
    FOREIGN KEY (parentItemID) REFERENCES items(itemID) ON DELETE CASCADE
);

Solution

  • You need to search substring in LOWER(note), but replace it in original. I don't know where you getting sometext from, assume that you can check it's length. In following example I'll use constant.

    UPDATE itemNotes  
    SET 
      note = SUBSTR(note, 0, INSTR(LOWER(note), 'sometext')) || 'abc' || SUBSTR(note, INSTR(LOWER(note), 'sometext')+sometext_len)
    WHERE 
      INSTR(LOWER(note), 'sometext') >= 0;
    

    !Note: It will work only for one replace at a time.