Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
select m.Main_Test_Name,sum(datediff(s.job_submit_date,s.job_assign_date))/count(s.Sample_Details_Id) as averagetimetocompleteTest from main_test m inner join sample_details s on m.Main_Test_Id=s.Main_Test_Id group by m.Main_Test_Id;



my query is working fine (but in some cases i have insert date of
job_assign_date
as
'0001-01-01 00:00:00'

same for
job_submit_date '0001-01-01 00:00:00'


but when thes dates are
'0001-01-01 00:00:00'
then i want set it as null

but it gives me error

What I have tried:

I have tried

as 1)
select m.Main_Test_Name,sum(datediff(s.job_submit_date , case job_submit_date when '0001-01-01 00:00:00' then null else s.job_submit_date,s.job_assign_date))/count(s.Sample_Details_Id) as averagetimetocompleteTest from main_test m inner join sample_details s on m.Main_Test_Id=s.Main_Test_Id group by m.Main_Test_Id;";



2)
select m.Main_Test_Name,sum(datediff(s.job_submit_date,if(s.job_assign_date='0001-01-01 00:00:00',null,s.job_assign_date) s.job_assign_date)/count(s.Sample_Details_Id) as averagetimetocompleteTest from main_test m inner join sample_details s on m.Main_Test_Id=s.Main_Test_Id group by m.Main_Test_Id;
Posted
Updated 31-Oct-18 1:11am
Comments
CHill60 31-Oct-18 5:44am    
"but it gives me error" ... what error?
When posting questions to do with databases it is ALWAYS a good idea to share with us your table structure(s), some sample data and your expected results (for the data provided)

1 solution

use
NULLIF


Returns a null value if the two specified expressions are equal. For example, SELECT NULLIF(4,4) AS Same, NULLIF(5,7) AS Different; returns NULL for the first column (4 and 4) because the two input values are the same. The second column returns the first value (5) because the two input values are different.


Syntax


NULLIF ( expression , expression )  
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900