Oracle SQL--选择除空白或有空格的条目外的所有条目

Oracle SQL--选择除空白或有空格的条目外的所有条目,sql,oracle,select,Sql,Oracle,Select,你好,下午好 我正在尝试编写一个查询,以选择多个客户ID。有时,无论出于何种原因,客户ID(PK)可能有一个空的客户名称,或者客户名称中有4或5个空格,但没有数字或字母(名称可以是任何数字或字母字符串,有时也可以是“-” 我怎样才能过滤掉它?除了我的其他WHERE子句之外,我当前的查询看起来像这样。我需要确保CustomerName不仅仅是一堆空格或空的。它需要有一些字母数字文本,或者-: SELECT cust.CustomerID, cust.CustomerName FROM

你好,下午好

我正在尝试编写一个查询,以选择多个客户ID。有时,无论出于何种原因,客户ID(PK)可能有一个空的客户名称,或者客户名称中有4或5个空格,但没有数字或字母(名称可以是任何数字或字母字符串,有时也可以是“-”

我怎样才能过滤掉它?除了我的其他WHERE子句之外,我当前的查询看起来像这样。我需要确保CustomerName不仅仅是一堆空格或空的。它需要有一些字母数字文本,或者-:

   SELECT cust.CustomerID, cust.CustomerName
   FROM cust
   WHERE cust.StartDate Between '01-Jun-2017' AND '01-Sep-2017' AND cust.CustomerID Like '%100%' Or cust.CustomerID Like '%200%'
但是,我想要的东西是这样的:

   AND CustomerName NOT LIKE '%  %' OR != ""
AND (CustomerName NOT LIKE '%  %' OR CustomerName != '')

任何建议都将不胜感激

我认为以下功能将帮助您解决问题:

TRIM( [ [ LEADING | TRAILING | BOTH ] trim_character FROM ] string1 )
此外,您可能还需要对上述内容进行总结

LENGTH( string1 )
请不要犹豫,要求我进一步澄清


Ted

我认为以下功能将帮助您解决问题:

TRIM( [ [ LEADING | TRAILING | BOTH ] trim_character FROM ] string1 )
此外,您可能还需要对上述内容进行总结

LENGTH( string1 )
请不要犹豫,要求我进一步澄清


Ted

我想你只需要把括号放在正确的地方:

SELECT cust.CustomerID, cust.CustomerName
FROM cust
WHERE cust.StartDate Between date '2017-06-01' and date '2017-09-01' and 
      (cust.CustomerID Like '%100%' Or cust.CustomerID Like '%200%')
like
模式要求客户id至少有100或200,因此不允许使用空格和空字符串

您可能还注意到,我更喜欢使用ISO标准格式YYYY-MM-DD的日期常量

或者,如果您愿意:

SELECT cust.CustomerID, cust.CustomerName
FROM cust
WHERE cust.StartDate Between date '2017-06-01' and date '2017-09-01' and 
      regexp_like(cust.CustomerID, '[12]00')

我想你只需要把括号放在正确的地方:

SELECT cust.CustomerID, cust.CustomerName
FROM cust
WHERE cust.StartDate Between date '2017-06-01' and date '2017-09-01' and 
      (cust.CustomerID Like '%100%' Or cust.CustomerID Like '%200%')
like
模式要求客户id至少有100或200,因此不允许使用空格和空字符串

您可能还注意到,我更喜欢使用ISO标准格式YYYY-MM-DD的日期常量

或者,如果您愿意:

SELECT cust.CustomerID, cust.CustomerName
FROM cust
WHERE cust.StartDate Between date '2017-06-01' and date '2017-09-01' and 
      regexp_like(cust.CustomerID, '[12]00')

类似这样的方法可以解决上述问题:

where customerId is not null
and customerId > ''
and customerId = , replace(customerId, ' ', '') -- this takes care of the spaces

类似这样的方法可以解决上述问题:

where customerId is not null
and customerId > ''
and customerId = , replace(customerId, ' ', '') -- this takes care of the spaces

我假设您在查询时希望执行以下操作:

   AND CustomerName NOT LIKE '%  %' OR != ""
AND (CustomerName NOT LIKE '%  %' OR CustomerName != '')
警告!这不会给您一个编译错误,但它不会像您期望的那样工作。Oracle将把“”解释为NULL,并且没有任何内容不等于NULL(也没有任何内容等于NULL)。因此,你的条件的第二部分总是错误的,这使得它毫无用处。要实现你想要的,你必须使用

OR CustomerName IS NOT NULL
或如上所述:

OR TRIM(CustomerName) IS NOT NULL

我假设您在查询时希望执行以下操作:

   AND CustomerName NOT LIKE '%  %' OR != ""
AND (CustomerName NOT LIKE '%  %' OR CustomerName != '')
警告!这不会给您一个编译错误,但它不会像您期望的那样工作。Oracle将把“”解释为NULL,并且没有任何内容不等于NULL(也没有任何内容等于NULL)。因此,你的条件的第二部分总是错误的,这使得它毫无用处。要实现你想要的,你必须使用

OR CustomerName IS NOT NULL
或如上所述:

OR TRIM(CustomerName) IS NOT NULL

最好使用正则表达式:

AND NOT REGEXP_LIKE(CustomerName, '^[[:space:]]*$')

最好使用正则表达式:

AND NOT REGEXP_LIKE(CustomerName, '^[[:space:]]*$')

使用
TRIM
函数怎么样?或者使用replace
replace('this a test','',''),coalesce(replace('','','','','x')作为ExcludeMe
为什么对'-'的注释也要对其进行特殊处理?@RadimBača如果可以,请查看我的更新。问题是,有时CustomerName字段被输入为“”或“”或“”,我不想要这些结果。@mathguy您能将其作为完整的响应发布吗?您的回答非常有效…我不知道我可以在select语句中输入TRIM(CustomerName)NOTNULL。你能给我看一下它的正确语法吗?使用
TRIM
函数怎么样?或者替换
replace('this a test','',''),coalesce(replace('','','','','x')作为ExcludeMe
为什么关于'-'你想对它做点特别的事吗?@RadimBača如果可以,请查看我的更新。问题是,有时CustomerName字段被输入为“”或“”或“”,我不想要这些结果。@mathguy您能将其作为完整的响应发布吗?您的回答非常有效…我不知道我可以在select语句中输入TRIM(CustomerName)NOTNULL。你能告诉我它的正确语法吗?谢谢。但我想我的问题可能措辞错误。第一个查询运行良好。我需要一个附加的AND条件,其中我正在使用NOT LIKE,但我不知道语法。问题是(从一开始就是)排除
customerid
null
或所有空格的行。这个答案与这个问题无关。谢谢。但我想我的问题可能措辞错误。第一个查询运行良好。我需要一个附加的AND条件,其中我正在使用NOT LIKE,但我不知道语法。问题是(从一开始就是)排除
customerid
null
或所有空格的行。此答案与问题无关。如果可以,请查看我的更新。问题是,有时CustomerName字段被输入为“”或“”或“”,我不想要这些结果。为什么需要
LENGTH()
<代码>修剪(…)不为空是所需的全部。是的,您是对的,可能不需要长度。回到TRIM,TRIM(TRIM中的两个“-”和TRIM(x中的两个“))?OP并不打算以任何特定的方式处理破折号
-
,他只是提到“有效”名称可能包括字母、数字,有时还有破折号。他们不需要被处理。只需要处理空间。然后:默认情况下,“修剪”将从两侧修剪(尽管在这个问题中这并不重要),而且默认情况下,“修剪”将修剪空间。所以
TRIM(customername)
单独使用,没有其他参数,就足够了!所以我不明白剩下的未决问题是什么!?如果可以,请查看我的更新。问题是,有时CustomerName字段被输入为“”或“”或“”,我不想要这些结果。为什么需要
LENGTH()
<代码>修剪(…)不可用