如何获取列有长空格(多个空格)

时间:2015-07-16 07:52:25

标签: sql oracle

我有以下结构(' '指的是空格):

name   description
---------------------
a         yes
b       '       '
c         ' '
d          null

我正在搜索一个查询,它给我的行包含空格,询问下面的结果。

 name   description
    ---------------------
    b       '       '
    c         ' '

此查询select * from tab1 where description =' ';会给我只有c,在我的查询中我有很多值有很长的空格。

4 个答案:

答案 0 :(得分:2)

您可以使用REGEXP_LIKE

with src as (select 'a' as name,'yes' as description from dual
union all 
select 'b','       ' from dual
union all
select 'c',' ' from dual
union all
select 'd',null from dual)
select * from src where regexp_like(description,'^[ ]+$'))
  

已编辑:添加了regexp_like(description,'^ [] + $')以仅考虑带空格的描述。如果有's','s'或's'格式的描述,则不会被选中。

答案 1 :(得分:1)

使用TRIM功能修剪空格。

select * from tab1 where TRIM(description) IS NULL; 

我没有测试过,但应该可以使用。

答案 2 :(得分:1)

使用此基本查询:

with sample_data(name, description) as (
  select 'a', 'yes' from dual union all 
  select 'b', '       ' from dual union all
  select 'c', ' ' from dual union all
  select 'd', null from dual
)
select * 
  from sample_data

您可以在以下where子句中选择以获得所需结果:

where regexp_like(description,'[ ]')); -- at least one space in the string
where regexp_like(description,'[ ]{2,')); -- two or more consecutive spaces
where regexp_like(description,'^[ ]+$')); -- just spaces of any length
where regexp_like(description,'^[ ]{2,}')); -- just paces of length 2 or more

如果你想要任何空白字符(例如制表符,垂直制表符,非空白空格等),你可以用任何一个[ ]命名字符类替换单个空格字符类[[:space:]]上面的条款。

答案 3 :(得分:0)

使用LIKE运算符

#ffffff
相关问题