尾随空格plsql

时间:2016-06-20 06:24:01

标签: sql oracle trailing

我需要找出带尾随空格的记录。我尝试使用Length(rtrim)构建查询,但是它没有返回所需的记录。

e.g。

abc "
abc       "
abc                                                               "

请协助

1 个答案:

答案 0 :(得分:5)

我认为你走在了正确的轨道上。以下示例将返回第二个和第三个文本,但不返回第一个文本。

SELECT * FROM
    (SELECT 'abc' AS text FROM DUAL
     UNION ALL
     SELECT 'abc       ' AS text FROM DUAL
     UNION ALL
     SELECT 'abc                        ' AS text FROM DUAL)
WHERE LENGTH(RTRIM(text)) != LENGTH(text);

对于包含可能有尾随空格的文本的列YOURTABLE的表YOURCOLUMN

SELECT * FROM
YOURTABLE
WHERE LENGTH(RTRIM(YOURCOLUMN)) != LENGTH(YOURCOLUMN);

如果您还有HORIZONTAL TABLINE FEEDCARRIAGE RETURN,则可以使用TRANSLATE函数将这些视为普通空格:

SELECT * FROM
    (SELECT 'abc' AS text FROM DUAL
     UNION ALL
     SELECT 'abc' || CHR(09) AS text FROM DUAL
     UNION ALL
     SELECT 'abc' || CHR(10) AS text FROM DUAL
     UNION ALL
     SELECT 'abc' || CHR(13) AS text FROM DUAL
     UNION ALL
     SELECT 'abc           ' AS text FROM DUAL)
WHERE LENGTH(RTRIM(TRANSLATE(text, CHR(09) || CHR(10) || CHR(13), ' '))) != LENGTH(text);