I have problem to read data from database. Could someone help me to write a suitable query for my purpose?
Select name1, name2
if (name2 start with "abcd" or "bcda" or "dasd" then skip 4 first Characters )
From TableName
where name2 like 'Input'
Lets have a glimpse of question : I want to get data of name2. Data that are similar to 'Input' but at the first I want to check if name2 started with "abcd" then I don't want to show 4 first Characters of name2 .
Edited : I forgot I should check 2 different parameters then I add an or to it and also I am working on an app for android and use SQlite.
I hope I could explain it clearly and at the sorry for my English .
Edit 2 : I have problem yet with code . It doesn't show 4 first letters very well but problem is here it returns this 4 first letters . This code is search part of word dictionary . I attached a picture to make it clear for you . all of these words started with 'das ' and I don't want to show them because I filtered it with below code .
c = mydb.rawQuery("select dId, case when substr(dWord, 1, 4) in ('das ', 'der ','die ') then substr(dWord, 5, length(dWord) - 4)else dWord end as dWord from german where dWord like '%"+search+"%'",null);
If your RDMBS supports a regexp function like regex_replace()
, that's straightforward:
regexp_replace(name2, '^(abcd)|(bcda)', '') as name2
This will work on Oracle, Postgres, MySQL 8.0, PrestoDB, BigQuery, DB2, Sybase (and probably more that I am not thinking of right now!).
In SQLIte, you can use string functions and a case expression:
select
name1,
case when substr(name2, 1, 4) in ('abcd', 'bcda')
then substr(name2, 5, length(name2) - 4)
else name2
end as name2
from mytable
where name2 = 'input'
Note: name2 like 'input'
is equivalent to name2 = 'input'
, since no wildcard comes into play.