2016-12-15 50 views
1

我想检查posgtres查询,在我的表中的情况下两个条件一个字段可能有两个值或者将为空或将单引号('')所以我尝试此查询,但没有执行空栏或空字段检查在rails中的postgres查询

CASE WHEN (items.item_code is NULL OR items.item_code = '') THEN 
items.name::text ELSE items.item_code::text END as item_code 

错误,如:

PG::Error: ERROR: syntax error at or near ")" LINE 1: ...HEN (items.item_code is NULL OR items.item_code =) THEN ite..

+1

“空或单引号”,你的意思是'NULL'或空字符串?因为单引号是完全不同的东西。 – sagi

+1

“它没有执行”;什么是错误? – Marth

+0

错误,如PG ::错误:错误:语法错误在或接近“)” LINE 1:... HEN(items.item_code为NULL或items.item_code =)然后... – amtest

回答

1

你有问题,在红宝石引号,而不是在SQL语句。 尝试更改为:

CASE 
    WHEN (coalesce(length(items.item_code),0) + length(items.item_code)) < 1 
    THEN items.name::text ELSE items.item_code::text 
END as item_code 

上面的代码做同样的条件检查你的,布塔空隙用引号。我想你可能应该像这样:

CASE WHEN (items.item_code is NULL OR items.item_code = \'\') THEN 
items.name::text ELSE items.item_code::text END as item_code 
+0

第二个工作正常,这是报价问题,谢谢 – amtest