1
SELECT 1 FROM dual where trim('X ') = 'X'

给出 1。

SELECT 1 FROM dual where trim(' ') = ''

不给出任何行。

有没有一种简单的方法来选择所有field缺少正则表达式的空格的行?

有没有办法将TRIM“欺骗”成TRIMming' '和给予''

4

1 回答 1

6

在 Oracle 中,空字符串是 NULL。所以传统的方法是这样的

SELECT 1 
  FROM dual 
 WHERE trim(' ') IS NULL

当然,这也将返回列所在的结果NULL。如果您想要列为非 NULL 且仅由空格组成的情况

SQL> ed
Wrote file afiedt.buf

  1  with t as (
  2    select 1 id, ' ' str from dual union all
  3    select 2, null from dual union all
  4    select 3, 'a' from dual
  5  )
  6  select *
  7    from t
  8   where trim(str) is null
  9*    and str is not null
SQL> /

        ID S
---------- -
         1

只是为了完善答案,如果您也想使用简单regexp_like

SQL> ed
Wrote file afiedt.buf

  1  with t as (
  2    select 1 id, ' ' str from dual union all
  3    select 2, null from dual union all
  4    select 3, 'a ' from dual
  5  )
  6  select *
  7    from t
  8*  where regexp_like( str, '^[[:space:]]+$' )
SQL> /

        ID ST
---------- --
         1
于 2012-06-20T13:36:04.940 回答