Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL Case in select statement for checking null

Tags:

sql

mysql

in MySQL query if I pass :

  case guides.Gud_Publish_Date       when null then "Unscheduled"       else "Forth Coming Titles"    end 

then it is considering all is null even the Gud_Publish_Date has value also. the full SQL statement is

SELECT guides.Gud_Id     , guides.Gud_Image     , guides.Gud_SubEditor     , guides.Gud_Reprint_Status     , guides.Gud_Publish_Date     , guides.Gud_Img_Chk     , guides.Gud_Published     , guides.Gud_View     , (         CASE guides.Gud_Publish_Date             WHEN NULL                 THEN "Unscheduled"             ELSE "Forth Coming Titles"             END         ) AS Schedules FROM guides 

Can anybody help me? Thanks in advance

like image 227
user1023242 Avatar asked Nov 12 '12 07:11

user1023242


People also ask

Is NULL in CASE statement MySQL?

The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END . For the first syntax, case_value is an expression.

How do I check if data is NULL in MySQL?

SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' '; The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value.

How do I query NULL in MySQL?

To search for column values that are NULL , you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression: mysql> SELECT * FROM my_table WHERE phone = NULL; To look for NULL values, you must use the IS NULL test.

How do you remove NULL values from a CASE statement?

ISNULL() Function, COALESCE() Function And CASE Statement There are three ways in which we can remove Null values in Table data and show them to the user. In our Table data, some columns contain null values. We can replace it using ISNULL() function , COALESCE() function, and CASE Statement.


2 Answers

try using IF

SELECT guides.Gud_Id     , guides.Gud_Image     , guides.Gud_SubEditor     , guides.Gud_Reprint_Status     , guides.Gud_Publish_Date     , guides.Gud_Img_Chk     , guides.Gud_Published     , guides.Gud_View     , IF(guides.Gud_Publish_Date IS NULL,'Unscheduled','Forth Coming Titles')               AS Schedules FROM guides 

or if you really want CASE

SELECT guides.Gud_Id     , guides.Gud_Image     , guides.Gud_SubEditor     , guides.Gud_Reprint_Status     , guides.Gud_Publish_Date     , guides.Gud_Img_Chk     , guides.Gud_Published     , guides.Gud_View     , (         CASE              WHEN guides.Gud_Publish_Date IS NULL             THEN 'Unscheduled'             ELSE 'Forth Coming Titles'         END       ) AS Schedules FROM guides 
like image 55
John Woo Avatar answered Sep 27 '22 21:09

John Woo


I found this -a couple of months- old post. Using COALESCE option as intended by Rajan, you can do,

SELECT guides.Gud_Id     , guides.Gud_Image     , guides.Gud_SubEditor     , guides.Gud_Reprint_Status     , guides.Gud_Publish_Date     , guides.Gud_Img_Chk     , guides.Gud_Published     , guides.Gud_View     , CASE COALESCE(guides.Gud_Publish_Date, 0)           WHEN 0 THEN 'Unscheduled'                  ELSE 'Forth Coming Titles'           END  AS Schedules FROM guides 

The code above assumes that guides.Gud_Publish_Date cannot take the value 0, which I can do because it is a date. If that weren't the case you can change 0 for another value that it cannot take; maybe your favorite float like 3.1415 or a null identifier 'null'.

like image 31
Diego-MX Avatar answered Sep 27 '22 20:09

Diego-MX