在 SQL 查询 (DB2) 中,如何在另一个字段中查找字段值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15328029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In a SQL query (DB2), how can I find a field value within another field?
提问by Steve L
In a SQL query (DB2), how can I find a field value within another field? For example, Field01 contains part number '1234'. Field02 contains 'this is my 1234 to keep'
在 SQL 查询 (DB2) 中,如何在另一个字段中查找字段值?例如,Field01 包含部件号“1234”。Field02 包含“这是我要保留的 1234”
How can I find or search for whatever is the value of Field01 within Field02?
如何在 Field02 中查找或搜索 Field01 的值?
I've tried LOCATE and LIKE and % % and Position but cannot seem to get it.
我试过 LOCATE 和 LIKE 以及 % % 和 Position 但似乎无法得到它。
I have been able to find Field01 in Field02 as long as it is in the beginning of Field02, but not if it is proceeded by other characters. This is the command I've used for this:
我已经能够在Field02中找到Field01,只要它在Field02的开头,但如果它是由其他字符进行的就不行。这是我用于此的命令:
CASE
WHEN LEFT ( TRIM ( FIELD02), CHARACTER_LENGTH ( TRIM ( FIELD01 ) ) ) =
TRIM ( WMCPIL ) THEN TRIM ( FIELD02)
ELSE '^'
END
回答by BellevueBob
回答by mvp
You can use LIKE
in following manner:
您可以通过LIKE
以下方式使用:
SELECT *
FROM mytable
WHERE field2 LIKE '%' || field1 || '%'
SQLFiddle demo(using PostgreSQL as SQLFiddle does not support DB2 yet, but this example should work on DB2 as well).
SQLFiddle 演示(使用 PostgreSQL 作为 SQLFiddle 尚不支持 DB2,但此示例也适用于 DB2)。
DB2 prefers to use CONCAT
to concatenate strings, but it also supports standard ||
as a synonym.
DB2 更喜欢使用CONCAT
连接字符串,但它也支持标准||
作为同义词。
回答by Pravin Dwiwedi
It is working fine, try it-
它工作正常,试试吧-
SELECT * FROM TAB1 WHERE POSITION(field1,field2,CODEUNITS16)<>0
SELECT * FROM TAB1 WHERE POSITION(field1,field2,CODEUNITS16)<>0