Tutorial :How to use Case Statement


Using C#.Net and MYSQL

I want to use a CASE Statement like SQL


SELECT vehicleno,          brandname,          CASE            WHEN inexpire < 0 THEN 'Expired'            ELSE inexpire          END AS inexpire   FROM   (SELECT Concat(plateno, vehicleno)   AS vehicleno,                  brandname,                  Datediff(inedate, Curdate()) AS inexpire           FROM   tb_car           WHERE  inedate < DATE_ADD(Curdate(), INTERVAL 30 DAY)           GROUP  BY vehicleno) AS tab1   

When I run the Query it is showing the output as

System.Byte[] instead of "Expired"

In the above query inexpire is column values

How can I solve this issue.....

Need Query Help


I think you would need to use the same datatype for both cases


case when inexpire < 0 then 'Expired' else 'inexpired' end as inexpire  


 case when inexpire < 0 then 1 else inexpire end as inexpire  


Actually I just did a quick test on my machine this mixing of datatypes doesn't cause an error but the result just shows as Blob in MySQL Workbench

SELECT    cast( CASE  WHEN inexpire < 0 THEN 'Expired' ELSE inexpire END as char) as inexpire  

stops this issue as suggested here. This should thus avoid the Byte[] issue


IF(inexpire < 0, 'Expired',inexpire) AS inexpire

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »