1

I'm trying to add link to my image when I return it from database but I don't want to add it when image is null. I tried CONCAT and CONCAT_WS but it didn't work

both of these didn't work:

SELECT id, name_en as name, CONCAT_WS("http://website.com/", image) as image FROM businesses

SELECT id, name_en as name, CONCAT("http://website.com/", image) as image FROM businesses
1
  • coalesce(object,'') essential replace null with empty string. Coalese takes the first non-null value in a series. dev.mysql.com/doc/refman/8.0/en/… Commented Dec 14, 2021 at 22:32

2 Answers 2

2

you can use ÌF flowcoltrol,`to determine iif image is NULL and if not add the url

SELECT id, name_en as name, IF(image IS NOT NULL, CONCAT("http://website.com/", image),"") as image 
FROM businesses
Sign up to request clarification or add additional context in comments.

4 Comments

That worked like a charm. can I add another query to support empty strings too not just null?.
use image IS NOT NULL OR image <> ''
Thank you so much. And I just checked your account and you look like you are very good in this. Can you recommend me a book about database?, I find it really interesting and really want to learn more about it. Thanks in advance.
look first at the links here stackoverflow.com/tags/mysql/info there are video tutorials and for beginners w3school and other lot s and lots, but look around this tag or the sql tag and read what is asked and the answers, they provide also good explanations and buzzwords to look for the manual also is a good starting point
0

What you're looking for is the COALESCE() function: Doc link below. It returns the first non-null value in a series. so in essence we replace Null with an empty string.

SELECT id, name_en as name, CONCAT("http://website.com/", coalesce(image,'')) as image 
FROM businesses

https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#function_coalesce

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.