Tutorial :How to use Case Statement



Question:

Using C#.Net and MYSQL

I want to use a CASE Statement like SQL

Query

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


Solution:1

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

e.g.

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

or

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

Edit

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


Solution:2

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
Previous
Next Post »