简体   繁体   中英

Oracle string search related query to find user names that matches search?

I am bit new to SQL help me to write a simple query

suppose table name is "Users" and below column name is "DomainUser"

ac\jason_mirabello  
mac\jason_thompson  
nyc\jason_graham  
jdb\jason_bates  

i got user name that comes after '\\' so i need to search through user name to find if a user exist so i need to exclude domain part

help me writing a query to achieve above in oracle

Try using like clause as below:

SELECT * 
FROM Users
WHERE DomainUser LIKE '%jason_graham'

You can use a LIKE operator:

SELECT * FROM users
 WHERE domainuser LIKE '%\jason_graham';

but I like using a regular expression here in case the backslash \\ doesn't exist:

SELECT * FROM users
 WHERE REGEXP_SUBSTR(domainuser, '[^\\]+$') = 'jason_graham';

The difficulty with both of the above queries is that neither will use an index. What you can do is create a function-based index which will work with the latter query:

CREATE INDEX domainuser_un ON users (REGEXP_SUBSTR(domainuser, '[^\\]+$'));

It might also be helpful to use LOWER() so that the index is "case-insensitive" (just make sure to convert the search term to lowercase too!) - assuming your usernames are not case-sensitive:

CREATE INDEX domainuser_un ON users (LOWER(REGEXP_SUBSTR(domainuser, '[^\\]+$')));

Here is an explanation of the regular expression I used.

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