Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify SQL result on the fly?

Tags:

c#

.net

sql

mysql

I have an SQL query :
SELECT DATEDIFF(deadline,CURDATE()) FROM tasks WHERE 1

The Result is : 3

How can I return the result as : 3 Days instead of 3

I know that I can manually append the string from my C# code something like :

 string result = getSqlresult();
 string result += " Days";

But I want to get the result directly as 3 Days from MySQL database.

The reason : I'm binding information directly to datagridview and therefore, In order to modify the result i need to iterate through all rows and update them. So to increase performance, I need to get the result directly from database as 3 Days instead of 3

Anyhelp would be highly appreciated

like image 706
Rafik Bari Avatar asked Apr 16 '26 05:04

Rafik Bari


1 Answers

you can concatenate the string Days into the result of DATEDIFF using CONCAT.

SELECT CONCAT(DATEDIFF(deadline,CURDATE()), ' Days') 
FROM tasks 
WHERE 1

if you are using old versions of MySQL, convert it to string so you will not get bolb result.

SELECT CONCAT(CAST(DATEDIFF(deadline,CURDATE()) AS CHAR(5)), ' Days') 
FROM tasks 
WHERE 1

UPDATE 1

SELECT  CASE 
            WHEN DATEDIFF(deadline,CURDATE()) >= 0
            THEN CONCAT(DATEDIFF(deadline,CURDATE()), ' Days')
            ELSE CONCAT('Expired since ', DATEDIFF(deadline,CURDATE()) * -1, ' Days')
        END
FROM    tasks
  • SQLFiddle Demo
like image 72
John Woo Avatar answered Apr 18 '26 20:04

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!