查找姓氏中包含特定字符的用户名

时间:2011-07-28 18:37:56

标签: sql regex oracle

在Oracle SQL中,我希望找到所有用户名,其姓氏包含一些特定字符,如('Z','X','D','F')或包含来自'A-的某些字符范围F”。

我试过了:

 SELECT user_last_name
    FROM   userbase
    WHERE  user_last_name LIKE '%A%' OR user_last_name LIKE '%F%' .

那我怎么能去做呢。

我写的facebook有问题,虽然它与它没有直接关系,因为它是我先生给出的例子。

更新:我尝试了上面的代码,它适用于很多OR,但我们如何定义一些搜索范围,如IN('Z','X','D', 'F')或IN('A-F')

2 个答案:

答案 0 :(得分:6)

如果您使用的是最新版本的Oracle,则应该能够使用正则表达式

SQL> ed
Wrote file afiedt.buf

  1   select first_name, last_name
  2     from employees
  3*   where regexp_like( last_name, 'Z|X|D|[A-F]' )
SQL> /

FIRST_NAME           LAST_NAME
-------------------- -------------------------
Ellen                Abel
Sundar               Ande
Mozhe                Atkinson
David                Austin
Hermann              Baer
Shelli               Baida
Amit                 Banda
Elizabeth            Bates
Sarah                Bell
David                Bernstein
Laura                Bissot
Harrison             Bloom
Alexis               Bull
Anthony              Cabrio
Gerald               Cambrault
Nanette              Cambrault
John                 Chen
Kelly                Chung
Karen                Colmenares
Curtis               Davies
Lex                  De Haan
Julia                Dellinger
Jennifer             Dilly
Louise               Doran
Bruce                Ernst
Alberto              Errazuriz
Britney              Everett
Daniel               Faviet
Pat                  Fay
Kevin                Feeney
Jean                 Fleaur
Tayler               Fox
Adam                 Fripp
Samuel               McCain
Allan                McEwen
Donald               OConnell
Eleni                Zlotkey

37 rows selected.

如果您希望搜索不区分大小写

SQL> ed
Wrote file afiedt.buf

  1   select first_name, last_name
  2     from employees
  3*   where regexp_like( last_name, 'Z|X|D|[A-F]', 'i' )
SQL> /

FIRST_NAME           LAST_NAME
-------------------- -------------------------
Ellen                Abel
Sundar               Ande
Mozhe                Atkinson
David                Austin
Hermann              Baer
Shelli               Baida
Amit                 Banda
Elizabeth            Bates
Sarah                Bell
David                Bernstein
Laura                Bissot
Harrison             Bloom
Alexis               Bull
Anthony              Cabrio
Gerald               Cambrault
Nanette              Cambrault
John                 Chen
Kelly                Chung
Karen                Colmenares
Curtis               Davies
Lex                  De Haan
<<snip>>
Matthew              Weiss
Jennifer             Whalen
Eleni                Zlotkey

93 rows selected.

答案 1 :(得分:0)

您可以使用INSTR测试字段中是否存在字符,如下所示:

SELECT
  user_last_name
FROM
  userbase
WHERE 0 < INSTR(user_last_name,'A')
   or 0 < INSTR(user_last_name,'B')
   ...repeat for each character you want to test for...