If Else condition in mysql to check null - mysql

How should I make if condition in mysql where time = null then I want result in NO else YES?
SELECT
case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) != NULL THEN
#Loss:='YES'
ELSE
#Loss:='NO'
END AS Loss
from Event01,(SELECT #Loss:= 0) AS Loss

Query
SET #Loss := 0;
SELECT
#Loss:= case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) IS NOT NULL
THEN 'YES'
ELSE 'NO' END
FROM Event01;

Try this, You have to check NULL using is clause.
SELECT
case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) is not NULL THEN
'YES'
ELSE
'NO'
END AS Loss
from Event01
Hope it will help.

Try this
SELECT case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) IS NULL THEN 'NO' ELSE 'YES' END AS Loss from Event01

Related

CASE Statement MYSQL with Condition

I got 2 CASE statements: First CASE statement is as follows:
Case When ((cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '')
AND (comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')) Then 'Pending'
ELSE 'Shipped' End AS shipment_status
The Second CASE statement is as follows:
Case When apbg_bal_pay_amt ='0' Then 'Received'
Else 'Pending' End AS payment_status
Iam looking to write one more CASE statement named OVERALL_Status. That is basically a combination of both this CASES (shipment_status and payment_status), which means if Shipment status is 'Shipped' AND Payment_Status is 'Received' then Overall_status is 'Completed' else 'Not Completed'. Can anyone please help me on this. I am really struck here. I tried the combination of both the CASES, but not working:
Case When (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') AND
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '') AND (apbg_bal_pay_amt != '0') Then
'Pending' ELSE 'Completed' End AS overall_status
There is two solutions as I see it:
1. You make the current query a subquery and add another CASE statement on the result of the subquery. (the pretty solution as I see it)
2. You add another WHEN to the existing case. Combining the clauses in the first two WHEN clauses to get the result. (probably the most optimized solution)
How about this?
(case when (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') and
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')
then 'Not Completed'
when apbg_bal_pay_amt = '0' Then 'Completed'
else 'Not Completed'
end) as overall_status

Nested case statement sql

I'm having issues with writing a case statement in SQL. My first question is: Is it possible to write the if statement below as a case statement in a SQL Query within the select statement?
If no, then please have a look at the case statement below and help/guide me to get into a valid format. Thanks and much appreciated!
IF (var1 = 1){
do this1;
IF (var 1 = 2){
Do this2;
}Else{
do something else1;
}
Else if (Var 1 = 3){
Do this3;
}Else{
Do something else2;
}
Here is my case statement. I know it doesn't work because it's not a valid case statement. Could someone kindly help me in making it a valid case statement. Thanks in advance.
SELECT
CASE
WHEN apple.type = 1 OR apple.type = 2
THEN basket.S1
ELSE
CASE
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
ELSE basket.S2
ELSE
CASE
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
END
END
FROM .....
WHERE .....
I think you are over complicating your case statement , looking at your first example you case statement should be fairly simple,
something like ....
SELECT CASE
WHEN #Var1 = 1 THEN 'Something 1'
WHEN #Var1 = 2 THEN 'Something 2'
WHEN #Var1 = 3 THEN 'Something 3'
ELSE 'Something Else'
END
FROM .....
WHERE .....
You case statement can we written something like this...
SELECT
CASE
WHEN apple.[type] IN (1,2)
THEN basket.S1
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
Since you're checking for nulls and substituting non-null values, you can make the query shorter by using the COALESCE function.
SELECT
CASE
WHEN apple.type IN (1, 2) THEN basket.s1
WHEN apple.type = 0 THEN COALESCE(basket.s2, basket.s1)
WHEN apple.type = 3 THEN COALESCE(basket.s3, basket.s1)
END
FROM ...

Select a 'virtual' column if another column has a value or is null in a SQL view

I want to select ALWAYS a column in my view, and set it with CASES.
example:
SELECT isDone
,doneN
from...
I have to set isDone in this way:
if done is 1 isDone is 'yes'
if done is 0 isDone is 'no'
(doneN is a column from another table, isDone instead doesn't exist in other tables, so it's a "virtual" column)
Thank you in advice
Is this what you want?
select (case when doneN = 1 then 'yes' else 'no' end) as isDoneN
. . .
You didn't specify whether 'done' will always be 0 or 1. If there are more values or you want to catch other values, use something like this:
select (case when done = 1 then 'yes'
when done = 0 then 'no'
else '' end) as isDone
, doneN
from ...
If the 'done' is constrained to be 0 or 1, you can use:
select (case when done = 1 then 'yes'
else 'np' end) as isDone
, doneN
from ...

using nested case condition in mysql query

if(qtype==3)
column=somecolumn;
else if(qtype==2)
{
if(open==0)
column=anycolumn;
else
column =somecolumn;
}
else{
acolumn,bcolumn,ccolumn //this else----|
} |
----------------|
I want to achieve the above nested if-else condition in my mysql query.where the condition will be based on one of the column.this is what i have done so far.
select qtype,open,(Case when qtype=2 then answer
Else (Case when qtype=3 then
(Case when open=0 then somecolumn
Else othercolumn End) End)
Else....//how to implement this else here)
i am in confusion how to integrate the last else part?
Thanks
The format is correct, but you switch the 2 and 3 compared to the if-else psuedocode at the begining:
select qtype,open,(Case when qtype=3 then answer
Else (Case when qtype=2 then
(Case when open=0 then somecolumn
Else othercolumn End) End))
Looking at the case syntax, here is what I came up with.
CASE qtype
WHEN 3 THEN column=somecolumn;
WHEN 2 THEN (CASE open WHEN 0 THEN column=anycolumn ELSE column = somecolumn)
ELSE othercolumn
END CASE

Mysql if condition for two columns

I want to check the following condition in MYSQL. Please help me to find the solution for the below condition
Condition :
If(fld_intime == null)
{
'InMiss'
}
else if(fld_outtime == null)
{
'Outmiss'
}
else
{
'Present'
}
How can i create this using select statement..
SELECT IF(fld_intime IS NULL, 'InMiss', IF(fld_outtime IS NULL, 'Outmiss', 'Present'))
are you looking for this:
select case when fld_intime is null
then 'InMiss'
when fld_outtime is null
then 'Outmiss'
else 'Present'
end as Remarks
from your_table
SELECT CASE
WHEN fld_intime is null THEN 'InMiss'
WHEN fld_outtime is null THEN 'OutMiss'
ELSE 'Present'
END AS fld_status from yourtable
writing sql examplee.. Use Case statement in your where query
select
case when fld_intime is null then 'InMiss'
when fld_outtime is null then 'Outmiss'
else 'Present' end
from yourTable;
Try this::
Select
CASE
WHEN fld_inTime is null
THEN 'InMiss'
WHEN fld_outTime is null
THEN 'outMiss'
ELSE
'Present'
END
from table