Sqlite: multiple update (find and replace) case insensitive

南楼画角 提交于 2019-12-31 02:48:06

问题


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
);

回答1:


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.




回答2:


Nosyara answer works great but it only removes 1 instance at a time.

I also tried this from here:

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

But when I have many occurence of sometext in a field note, then only the first on is removed. Based on a comment on this question, I ended up using python to replace the sqlite, see here



来源:https://stackoverflow.com/questions/56242338/sqlite-multiple-update-find-and-replace-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!