1

I am trying to check two condition with case in posgtres query, in my table one field may have two values either will be empty or will be single quote('') so i tried this query but it didn't execute

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

Error like:

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

11
  • 1
    "Empty or single quote" , do you mean NULL or empty string? because single quote is a whole different thing. Commented Dec 15, 2016 at 8:32
  • 1
    "It didn't execute"; what's the error? Commented Dec 15, 2016 at 8:33
  • Error like PG::Error: ERROR: syntax error at or near ")" LINE 1: ...HEN (items.item_code is NULL OR items.item_code = ) THEN ite... Commented Dec 15, 2016 at 8:35
  • you probabky gave php statement with single quotes - right?.. Commented Dec 15, 2016 at 8:36
  • 1
    then it was quotes in your ruby - check out sql in my answer Commented Dec 15, 2016 at 8:52

1 Answer 1

2

you have problem with quotes in ruby, not in sql statement. try changing to:

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

the code above does same condition check as yours, buta voids using quotes. I suppose your could should be smth like:

CASE WHEN (items.item_code is NULL OR items.item_code = \'\') THEN
items.name::text ELSE items.item_code::text END as item_code
Sign up to request clarification or add additional context in comments.

1 Comment

That second one works fine, that was the quotes issue, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.