4

I'm not sure if this scenario is related only to Coldfusion or Mysql.

If a column that is of type TEXT is compared to an empty string value if it is not equal, this code works:

column > ""

but not this one:

column != ""

In my SQL code, let's say the database column attachment with a TEXT type has strings contained in it. The Form value of the Input Attachment (:attachment) is empty (I think it will refer to "").

attachment = ( 
    CASE 
        WHEN :attachment = "" AND attachment = "" 
            THEN 1 
        WHEN :attachment = "" AND attachment != "" 
            THEN 2 
        WHEN :attachment != "" AND attachment = "" 
            THEN 3
        WHEN :attachment != "" AND attachment != "" 
            THEN 4 
        ELSE attachment 
    END 
)

this code would return attachment = 3.

However, if I use this SQL code:

attachment = ( 
    CASE 
        WHEN :attachment = "" AND attachment = "" 
            THEN 1 
        WHEN :attachment = "" AND attachment > "" 
            THEN 2 
        WHEN :attachment > "" AND attachment = "" 
            THEN 3
        WHEN :attachment > "" AND attachment > "" 
            THEN 4 
        ELSE attachment 
    END 
)

This code returns attachment = 2 which is what I really am expecting to have.

So, what's the best way to compare a column if it is equal or if it is not equal to an empty string? I rarely use NULL values because when FORM are saved, they input empty strings.

8
  • Possible duplicate of Checking for an empty field with MySQL Commented Jun 3, 2017 at 0:53
  • Don't use double quotes to compare strings in the database change all of then to single quotes and test it again! Commented Jun 3, 2017 at 0:53
  • Why shouldn't I use double quotes? @JorgeCampos Commented Jun 3, 2017 at 0:55
  • @Pyromonk I'm checking if it is not equal, not if it is equal. I check the link you tagged and I didn't seem to find the exact answer to my question. Commented Jun 3, 2017 at 0:57
  • 1
    SQL ANSI standards. Double quotes is for identifiers as if you are creating a new column name. Single quotes is for string comparison. Commented Jun 3, 2017 at 0:58

2 Answers 2

3

I believe you should use <> instead of != and also use single quotation instead of double quotation e.g.

WHEN :attachment <> '' AND attachment <> ''
Sign up to request clarification or add additional context in comments.

1 Comment

Neither of these things should actually be relevant in the MySQL dialect. Double quotes for strings are accepted unless the server has ANSI_QUOTES enabled, and if it did, these queries would throw an error. For inequality, != and <> are equivalent.
0

Warning: NEVER use column > ''

I just had some cases with MySql 5.7, where column > '' resulted in FALSE, but column < '' resulted in TRUE!!

So better check for LENGTH(column)

Comments

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.