SQL DB2 自动修剪尾随空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1592390/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:04:51  来源:igfitidea点击:

DB2 automatic trim tailing whitespace

sqldb2

提问by Fuangwith S.

This is data in TB_USER table

这是 TB_USER 表中的数据

USER_USERID    
 --------------     
 A111           
 A9999          
 ADMIN          
 AHO            
 AHO2           
 AHO3           
 AHO4     

...and schema of TB_USER table is

...和 ​​TB_USER 表的架构是

 COLUMN_NAME           DATA_TYPE  DATA_LENGTH  
 --------------------  ---------  ----------- 
 USER_USERID           VARCHAR    15        

When I execute following SELECT statement:

当我执行以下 SELECT 语句时:

 SELECT USER_USERID FROM TB_USER WHERE USER_USERID = 'ADMIN '

...and:

...和:

 SELECT USER_USERID FROM TB_USER WHERE USER_USERID = 'ADMIN'

DB2 retreive same output following

DB2 检索以下相同的输出

USER_USERID    
 -------------- 
 ADMIN   

How to fix this problem?

如何解决这个问题?

回答by Michael Sharek

You should use the RTRIMor TRIMfunction.

您应该使用RTRIMTRIM函数。

SELECT USER_USERID FROM TB_USER WHERE RTRIM(USER_USERID) = 'ADMIN'

回答by Michael Hvizdos

This behaviour is as designed - equality predicates for string comparisons won't consider trailing spaces. As Rakesh suggested, the LIKE predicate will consider the trailing spaces. It depends upon your use case which you should go with.

此行为是按设计的 - 字符串比较的相等谓词不会考虑尾随空格。正如 Rakesh 所建议的,LIKE 谓词将考虑尾随空格。这取决于您应该使用的用例。

Relevant documentation:

相关文档:

DB2 9.7 Infocenter - Assignments and comparisons

DB2 9.7 信息中心 - 分配和比较

"When comparing character strings of unequal lengths, the comparison is made using a logical copy of the shorter string, which is padded on the right with blanks sufficient to extend its length to that of the longer string. This logical extension is done for all character strings, including those tagged as FOR BIT DATA."

"当比较长度不等的字符串时,比较是使用较短字符串的逻辑副本进行的,该副本在右侧填充足够的空格以将其长度扩展到较长字符串的长度。此逻辑扩展对所有字符进行字符串,包括标记为 FOR BIT DATA 的字符串。

DB2 9.7 Infocenter - LIKE predicate

DB2 9.7 信息中心 - LIKE 谓词

"The LIKE predicate searches for strings that have a certain pattern. The pattern is specified by a string in which the underscore and the percent sign may have special meanings. Trailing blanks in a pattern are part of the pattern."

“LIKE 谓词搜索具有特定模式的字符串。该模式由一个字符串指定,其中下划线和百分号可能具有特殊含义。模式中的尾随空格是该模式的一部分。

回答by Rakesh Juyal

Instead of

代替

SELECT USER_USERID FROM TB_USER WHERE USER_USERID = 'ADMIN '

Use

SELECT USER_USERID FROM TB_USER WHERE USER_USERID like 'ADMIN '