一个查询,让我具体说明字母顺序

时间:2014-01-15 15:44:39

标签: oracle

我有一个名为telephone_contacts的表,其中包含两列:

telephone_contacts (
  Name    varchar(100)
  Numbers number(20)
)

name包含大约20,000行。

我想按字母过滤名称,例如:

我想要一个只能获得前6个字母(A , B, C , D ,E ,F G)

的查询

然后,查询得到最后6个字母(U,V,W,X,Y,Z)

编辑:

示例:列名包含以下数据:
安倍,车,夜,范围,鸡,动物园,whatsapp,脸谱,viber阿杜,阿拉姆,自行车,男,狗,蛋
我想要一个只获得(A , B, C , D ,E ,F G)的查询,以便得到结果
abe,care,chicken facebook,adu,aramt,bike,dog,egg
其余的被忽略

1 个答案:

答案 0 :(得分:2)

如果是连续的,请使用BETWEEN

CHAR版

SELECT * FROM telephone_contacts WHERE SUBSTR(UPPER(Name),1) BETWEEN 'A' and 'G'

SELECT * FROM telephone_contacts WHERE SUBSTR(UPPER(Name),1) BETWEEN 'U' and 'Z'

ASCII版

SELECT * FROM telephone_contacts WHERE ASCII(SUBSTR(UPPER(Name),1)) BETWEEN 65 and 65+6-1

SELECT * FROM telephone_contacts WHERE ASCII(SUBSTR(UPPER(Name),1)) BETWEEN 90-6+1 and 90

进行测试:

WITH my_view AS
  (SELECT chr(level+65-1) my_text FROM dual CONNECT BY level <=26
  )
SELECT * FROM my_view WHERE SUBSTR(UPPER(my_text),1) BETWEEN 'A' AND 'G';