简体   繁体   English

案例陈述在Oracle中没有按预期工作

[英]Case statement not working as expected in Oracle

I have a field in the database that contains a lot of empty/null rows. 我在数据库中有一个包含很多空/空行的字段。 I am trying to create a case statement that changes this to "Empty Field" or something similar so that when it is displayed to the user, they will have an indication that the row has no data for that column but for some reason it is not working. 我正在尝试创建一个case语句,将其更改为“Empty Field”或类似的东西,以便在向用户显示时,它们将指示该行没有该列的数据但由于某种原因它不是工作。 When I select the data without the case statement, I get either a string or an empty field as should be. 当我选择没有case语句的数据时,我会得到一个字符串或一个空字段。 When I add the case statement, every row comes back as {null} regardless to what is in that column. 当我添加case语句时,无论该列中的内容如何,​​每一行都返回{null} I have used similar case statements many times but this has me baffled. 我曾经多次使用类似的案例陈述,但这令我感到困惑。

SELECT CASE
         WHEN col_name = TRIM('') THEN 'Empty Field'
         END col_name
FROM table_name

I also tried LIKE in place of the equals sign as well as NULL instead of TRIM('') 我也试过LIKE代替等号以及NULL而不是TRIM('')

working solution: 工作方案:

SELECT NVL(TRIM(col_name), 'Empty Field') AS col_name
FROM table_name

In Oracle, '' in VARCHAR context is treated as NULL 在Oracle中, ''在VARCHAR上下文中被视为NULL

You can just use NVL(col_name,'Default Value') 你可以使用NVL(col_name,'默认值')

select NVL(col_name, 'Empty Field') from table_name

TRIM('') is NULL in Oracle SQL, so has no effect. Oracle SQL中的TRIM('')为NULL,因此无效。

SQL> set null NULL
SQL> select trim('') from dual;

T
-
N
U
L
L

As far as your CASE statement, the problem lies in your use of equalty with NULL. 就CASE语句而言,问题在于你使用NULL等于。 You cant say where NULL = anything, it is false. 你不能说NULL =什么,它是假的。 NULL is not equal to anything, not even itself. NULL不等于任何东西,甚至不是自己。 You need to use: 你需要使用:

WHERE col_name IS NULL

IS NULL and = NULL are not the same. IS NULL和= NULL不一样。

SQL> select case when id = trim('') then 'Empty' end from nulls;

CASEW
-----
NULL

SQL> select case when id = null then 'Empty' end from nulls;

CASEW
-----
NULL

SQL> select case when id is null then 'Empty Field' end from nulls;

CASEW
-----
Empty Field      <--- DATA!

If this does not work, perhaps you have spaces in your data. 如果这不起作用,也许您的数据中有空格。 Test that by using SQLPLUS - SET NULL NULL command I showed to verify it is truly NULL. 测试通过使用SQLPLUS - SET NULL NULL命令我显示验证它是真正的NULL。

You should use nvl() as codenheim suggested, but further to the answers about your original solution, you have no else clause - so nothing is matching against = null and you never get Empty field , but everything that doesn't match is then defaulted to null anyway. 你应该使用nvl()作为codenheim的建议,但是对于你原来的解决方案的答案,你没有else子句 - 所以没有任何东西与= null匹配而你永远不会得到Empty field ,但是那些匹配的东西然后是默认的无论如何要为空。 You'd need to do: 你需要这样做:

SELECT CASE
     WHEN col_name IS NULL THEN 'Empty Field'
     ELSE col_name
     END AS col_name
FROM table_name

But that would be the same as 但那将是一样的

SELECT NVL(col_name, 'Empty Field') AS col_name
FROM table_name

You suggested that still isn't working but weren't very specific; 你建议仍然没有工作,但不是很具体; but your original use of trim makes me wonder if you have some values that are not null but only consist of spaces - so they would still just be spaces afterwards. 但是你对trim的原始使用让我想知道你是否有一些非空值但只包含空格的值 - 所以它们之后仍然只是空格。 If that is the case and you want to treat all-spaces as null then you could do: 如果是这种情况,并且您希望将所有空格视为null,那么您可以执行以下操作:

SELECT NVL(TRIM(col_name), 'Empty Field') AS col_name
FROM table_name

If you have other whitespace - just a tab perhaps - then you could use a regular expression to strip that out instead, eg: 如果你有其他空格 - 也许只是一个tab - 那么你可以使用正则表达式去除它,例如:

SELECT NVL(REGEXP_REPLACE(col_name, '^[[:space:]]+$', NULL),
    'Empty Field') AS col_name
FROM table_name;

SQl Fiddle demo . SQl小提琴演示

The correct thing to do is to use codenheim example.But for the sake of clarity when you check a NULL column in oracle you have to use IS NULL 正确的做法是使用codenheim示例。但是为了清楚起见,当您在oracle中检查NULL列时,您必须使用IS NULL

SELECT CASE
     WHEN col_name IS NULL THEN 'Empty Field'
     ELSE col_name
     END AS col_name
FROM table_name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM