StealthRT
StealthRT

Reputation: 10542

mySQL space before field value

I am trying to put a space in if the value is not blank but it seems to only output a number when i run the query?

CONCAT(address, IF(address2 = '', '', ' ' & address2), ' ', city, ', ', state, ' ', zip) AS theAddress

The query output for the above is:

1234 N. Shore Ave.0 Burbank, CA 41577

The output should look like this:

1234 N. Shore Ave. Apartment 223 Burbank, CA 41577

What would i be doing incorrectly here?

Upvotes: 2

Views: 83

Answers (2)

pjama
pjama

Reputation: 3044

Try this:

CONCAT(address, IF(address2 = '', '', CONCAT(' ', address2)), ' ', city, ', ', state, ' ', zip) AS theAddress

I think what your code is doing is a Logical AND between a space and address2

Upvotes: 4

John Woo
John Woo

Reputation: 263723

I think it should be another concat not &

CONCAT(address, IF(address2 = '', '', CONCAT(' ',  address2)), ' ', city, ', ', state, ' ', zip) AS theAddress
                                         ^ here

' ' & address2 returns 0 and 1 (true or false)

Upvotes: 2

Related Questions