Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql concate check not null

Tags:

mysql

SELECT Concat(mp.cHospital ,',',mp.cHospital1,',',mp.cHospital2) as Hospital FROM TBL

I dont want to return null value,

how to check not null or how to make isset cond in query

like image 765
Bharanikumar Avatar asked Feb 13 '10 18:02

Bharanikumar


People also ask

What is Concat_ws?

The CONCAT_WS() function adds two or more strings together with a separator.

IS NULL function in MySQL?

The MySQL ISNULL() function is used for checking whether an expression is NULL or not. This function returns 1 if the expression passed is NULL, else it returns 0. The ISNULL() function accepts the expression as a parameter and returns an integer a value 0 or 1 depending on the parameter passed.

What is the difference between the concat function and the Concat_ws function?

Both CONCAT() and CONCAT_WS() functions are used to concatenate two or more strings but the basic difference between them is that CONCAT_WS() function can do the concatenation along with a separator between strings, whereas in CONCAT() function there is no concept of the separator.


2 Answers

By definition (almost)any operation with NULL will result in NULL as NULL means "undfined". I interpret your question that either cHospital or cHospital1 or cHospital3 might be NULL which you want to check. The question is: what should happen? You want just the field replaced with an empty stirng and then the concat or all?

I assume the first. that might look like this:

SELECT Concat(
    IFNULL(mp.cHospital, ''),
    ',',
    IFNULL(mp.cHospital1,''),
    ',',
    IFNULL(mp.cHospital2,'')) AS Hospital
FROM TBL

IFNULL returns the first part, unless its NULL where it returns the second part(empty string here).

http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_ifnull

like image 96
johannes Avatar answered Sep 29 '22 06:09

johannes


I recommend using CONCAT_WS function. This works according to what you need. Would be something like

SELECT CONCAT_WS(',', mp.cHospital, mp.cHospital1, mp.cHospital2) as Hospital FROM TBL

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws

Greetings!!

like image 21
prog3712 Avatar answered Sep 29 '22 07:09

prog3712