简体   繁体   English

SQL不区分大小写的IN搜索

[英]SQL Case insensitive IN search

I have this Table "Table" with content: 我有此表"Table" ,其内容为:

+--------+
| Serial |
+--------+
| d100m  | <- expected result
| D100M  | <- expected result
| d200m  | <- expected result
| d300L  |
| D400R  |
+--------+

There are case inaccurate serial numbers stored. 大小写不正确的情况下存储的序列号。

Currently I am selecting there with a statement like 目前,我正在选择类似这样的声明

SELECT Serial FROM Table WHERE Serial LIKE 'D100M' OR Serial LIKE 'D200M';

But isn't there a easier way instead of OR Serial LIKE OR Serial LIKE OR Serial LIKE there are alomst 30 numbers i have to compare with. 但是,除了OR Serial LIKE OR Serial LIKE OR Serial LIKE还有没有更简单的方法,我必须比较30个数字。

Something like this 像这样

SELECT Serial FROM Table WHERE Serial LIKE IN ('D100M', 'D200M')

The easiest way would be: 最简单的方法是:

SELECT Serial 
FROM Table 
WHERE upper(Serial) in ('D100M', 'D200M');

That won't however use an index on the serial column. 但是,这不会在serial列上使用索引。

So if performance is a concern you would need to create an index on upper(serial) . 因此,如果要考虑性能,则需要在upper(serial)创建一个索引。

SELECT Serial FROM Table WHERE Serial IN ('D100M', 'D200M',<addAllSerialsHereCommaSeparated>)

更新:如果应该大写检查所有序列号,则可以使用:

SELECT Serial FROM Table WHERE upper(Serial) IN ('D100M', 'D200M',<addAllSerialsHereCommaSeparated>)

as long as you do not use wildcards or other like operator characters, you can use this script: 只要您不使用通配符或其他like运算符的字符,就可以使用以下脚本:

SELECT Serial FROM Table WHERE upper(Serial) IN ('D100M', 'D200M')

other wise, you need a full text search. 否则,您需要全文搜索。

I assume that you want to get the records with ignoring the case. 我假设您想不考虑案件而获得记录。 You can use upper or lower function and do something like: 您可以使用上层或下层功能并执行以下操作:

SELECT Serial FROM Table WHERE Upper(Serial) IN ('D100M', 'D200M')

Or 要么

SELECT Serial FROM Table WHERE Lower(Serial) IN ('d100m', 'd200m')

Use the below query format: 使用以下查询格式:

select * from dual where upper(DUMMY) in (SELECT upper(x.split_values)
FROM
  (WITH T AS
  (SELECT 'A,B,C,D,E,F' STR FROM DUAL
  )
SELECT REGEXP_SUBSTR (STR, '[^,]+', 1, LEVEL) SPLIT_VALUES
FROM T
  CONNECT BY LEVEL <=
  (SELECT LENGTH (REPLACE (STR, ',', NULL)) FROM T
  )
  ) x
)

The query in the IN clause converts your comma separated list to rows...the final selection in x.split values is converted to upper and then returned as columns. IN子句中的查询将您的逗号分隔列表转换为行...将x.split值中的最终选择转换为上层,然后作为列返回。

finally it is supplied to IN clause as a mini table... 最后将其作为迷你表提供给IN子句...

This is Oracle Specific. 这是Oracle特定的。

in case of MS SQL this link may be useful to perform same ops...: http://sqljason.com/2010/05/converting-single-comma-separated-row.html 如果是MS SQL,此链接对于执行相同的操作可能很有用...: http : //sqljason.com/2010/05/converting-single-comma-separated-row.html

you may check following: 您可以检查以下内容:

SELECT Serial
FROM Table
WHERE (Serial collate SQL_latin1_general_cp1_cs_as)
      IN ('D100M', 'D200M', 'd200m');

It should get correct and desired information. 它应该获得正确和所需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM