DB2 区分大小写
我很难使我的 DB2 (AS/400) 查询不区分大小写。
例如:
SELECT *
FROM NameTable
WHERE LastName = 'smith'
不会返回任何结果,但以下返回 1000 个结果:
SELECT *
FROM NameTable
WHERE LastName = 'Smith'
我读过将 SortSequence/SortType 放入连接字符串中,但没有运气......有人有这方面的经验吗?
编辑:
这是存储过程:
BEGIN
DECLARE CR CURSOR FOR
SELECT T . ID ,
T . LASTNAME ,
T . FIRSTNAME ,
T . MIDDLENAME ,
T . STREETNAME || ' ' || T . ADDRESS2 || ' ' || T . CITY || ' ' || T . STATE || ' ' || T . ZIPCODE AS ADDRESS ,
T . GENDER ,
T . DOB ,
T . SSN ,
T . OTHERINFO ,
T . APPLICATION
FROM
( SELECT R . * , ROW_NUMBER ( ) OVER ( ) AS ROW_NUM
FROM CPSAB32.VW_MYVIEW
WHERE R . LASTNAME = IFNULL ( @LASTNAME , LASTNAME )
AND R . FIRSTNAME = IFNULL ( @FIRSTNAME , FIRSTNAME )
AND R . MIDDLENAME = IFNULL ( @MIDDLENAME , MIDDLENAME )
AND R . DOB = IFNULL ( @DOB , DOB )
AND R . STREETNAME = IFNULL ( @STREETNAME , STREETNAME )
AND R . CITY = IFNULL ( @CITY , CITY )
AND R . STATE = IFNULL ( @STATE , STATE )
AND R . ZIPCODE = IFNULL ( @ZIPCODE , ZIPCODE )
AND R . SSN = IFNULL ( @SSN , SSN )
FETCH FIRST 500 ROWS ONLY )
AS T
WHERE ROW_NUM <= @MAXRECORDS
OPTIMIZE FOR 500 ROW ;
OPEN CR ;
RETURN ;
I'm having great difficultly making my DB2 (AS/400) queries case insensitive.
For example:
SELECT *
FROM NameTable
WHERE LastName = 'smith'
Will return no results, but the following returns 1000's of results:
SELECT *
FROM NameTable
WHERE LastName = 'Smith'
I've read of putting SortSequence/SortType into your connection string but have had no luck... anyone have exepierence with this?
Edit:
Here's the stored procedure:
BEGIN
DECLARE CR CURSOR FOR
SELECT T . ID ,
T . LASTNAME ,
T . FIRSTNAME ,
T . MIDDLENAME ,
T . STREETNAME || ' ' || T . ADDRESS2 || ' ' || T . CITY || ' ' || T . STATE || ' ' || T . ZIPCODE AS ADDRESS ,
T . GENDER ,
T . DOB ,
T . SSN ,
T . OTHERINFO ,
T . APPLICATION
FROM
( SELECT R . * , ROW_NUMBER ( ) OVER ( ) AS ROW_NUM
FROM CPSAB32.VW_MYVIEW
WHERE R . LASTNAME = IFNULL ( @LASTNAME , LASTNAME )
AND R . FIRSTNAME = IFNULL ( @FIRSTNAME , FIRSTNAME )
AND R . MIDDLENAME = IFNULL ( @MIDDLENAME , MIDDLENAME )
AND R . DOB = IFNULL ( @DOB , DOB )
AND R . STREETNAME = IFNULL ( @STREETNAME , STREETNAME )
AND R . CITY = IFNULL ( @CITY , CITY )
AND R . STATE = IFNULL ( @STATE , STATE )
AND R . ZIPCODE = IFNULL ( @ZIPCODE , ZIPCODE )
AND R . SSN = IFNULL ( @SSN , SSN )
FETCH FIRST 500 ROWS ONLY )
AS T
WHERE ROW_NUM <= @MAXRECORDS
OPTIMIZE FOR 500 ROW ;
OPEN CR ;
RETURN ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不这样做:
如果您担心性能(即查询不使用索引),请记住 DB2 有函数索引,您可以阅读 此处。因此本质上,您可以在
upper(LastName)
上创建索引。编辑
要执行我在评论中讨论的调试技术,您可以执行以下操作:
然后在 SP 中,您可以将消息插入到此表中以进行调试:
然后在 SP 运行后,您可以从此日志表中选择以查看哪些内容发生了。
Why not do this:
If you're worried about performance (i.e. the query not using an index), keep in mind that DB2 has function indexes, which you can read about here. So essentially, you can create an index on
upper(LastName)
.EDIT
To do the debugging technique I discussed in the comments, you could do something like this:
Then in your SP, you can insert messages to this table for debugging purposes:
Then after the SP runs, you can select from this log table to see what happened.
如果您希望在过程中不区分大小写,请尝试在其中使用此选项:
您还应该创建一个索引来支持它的性能。当您将
*LANGIDSHR
作为连接属性时创建索引,然后共享权重索引应该可用于以后的作业。 (有多种方法可以使适当的设置生效。)*LANGIDSHR
与您的作业的语言 ID 相关。字符集中可能被视为“等于”的字符(例如“A”和“a”或“ü”和“u”)应被赋予相同的权重(共享),以便一起选择。If you want case-insensitive in your procedure, try using this option in it:
You should also create an index to support it for performance. Create the index when you have
*LANGIDSHR
as a connection attribute, and the shared-weight index should then be available to later jobs. (There are various ways to get the appropriate setting into effect.)*LANGIDSHR
relates to the language-ID for your jobs. Characters in the character set that might be considered as "equals", such as 'A' and 'a' or 'ü' and 'u', should be given equal weights (shared) and so select together.当我想要不区分大小写的搜索时,我做了类似的事情。我使用了
UPPER(mtfield) = 'SEARCHSTRING'
。我知道这有效。I did something similar when I wanted a case insensitive search. I used
UPPER(mtfield) = 'SEARCHSTRING'
. I know this works.请参阅:https://stackoverflow.com/a/47181640/5507619
数据库设置
您可以设置一个数据库配置设置在 数据库创建。但它是基于 unicode 的。
这对我有用。正如您所看到的,..._S2 也忽略大小写。
使用 较新的标准版本,它应该如下所示:
排序规则关键字:
UCA400R1
= Unicode 标准 4.0 = CLDR 版本 1.2UCA500R1
= Unicode 标准 5.0 = CLDR 版本 1.5.1CLDR181
= Unicode 标准 5.2 = CLDR 版本 1.8.1如果您的数据库已创建,则应该有一种方法更改设置。
我确实在执行此操作时遇到问题,但据我所知它应该有效。
生成的表行
其他选项例如 生成大写行:
See: https://stackoverflow.com/a/47181640/5507619
Database setting
There is a database config setting you can set at database creation. It's based on unicode, though.
This worked for me. As you can see, ..._S2 ignores case, too.
Using a newer standard version, it should look like this:
Collation keywords:
UCA400R1
= Unicode Standard 4.0 = CLDR version 1.2UCA500R1
= Unicode Standard 5.0 = CLDR version 1.5.1CLDR181
= Unicode Standard 5.2 = CLDR version 1.8.1If your database is already created, there is supposed to be a way to change the setting.
I do have problems executing this, but for all I know it is supposed to work.
Generated table row
Other options are e.g. generating a upper case row: