Mysql if condition for two columns - mysql

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

Related

Mysql: If a field in a record is empty I want the stored procedure to return N/A as value

I have a table in MySQL where in a record a field (Default: None) does not have any value.
I'm using a stored procedure to select values from this table and I when this field has no value I should get the value N/A.
I tried the following code but I get the field with no value.
SELECT md.coding_id,
md.patient_id,
md.implant_date,
(case
when md.device_and_implant_description = ''
then 'N/A'
when md.device_and_implant_description !=0
then md.device_and_implant_description
end) as device_and_implant_description
FROM medical_devices_mapping as md
WHERE md.patient_id = p_id
The p_id value is given by the user.
This is the result:
This is the structure of my table:
Please use NUll as well as "".
If you want to use NULL or '' only then before storing data, make sure that you are storing null or empty string then accordingly use condition.
Use REGEXP condition instead of ''
SELECT md.coding_id, md.patient_id, md.implant_date,
CASE WHEN md.device_and_implant_description REGEXP '^[A-Za-z0-9]+$'
THEN md.device_and_implant_description
ELSE 'N/A'
END AS device_and_implant_description,
md.device_and_implant_description
FROM medical_devices_mapping md
WHERE md.patient_id = p_id
Maybe you should invert your logic
case when trim(md.device_and_implant_description) is not null then md.device_and_implant_description
else 'N/A'
end
I found the solution using COALESCE().
The proposed solution is the following:
SELECT md.coding_id,
md.patient_id,
md.implant_date,
(CASE when COALESCE(md.device_and_implant_description, '') !=''
then md.device_and_implant_description
when COALESCE(md.device_and_implant_description, '') =''
then 'N/A'
end) as device_and_implant_description
FROM medical_devices_mapping as md
WHERE md.patient_id = p_id

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

How to select last column without Null in mySQL

I am facing an issue I cannot seem to find the solution of anywhere. I have a table in mySQL with following columns:
RoleDuty1
RoleDuty2
RoleDuty3
RoleDuty4
RoleDuty5
RoleDuty6
RoleDuty7
This table has just one row or record. I want to see which is the last column in the table which is Not NULL? meaning that I want to dynamically get Column Name RoleDuty7 as an answer from mySQL, since all of the columns have some value other than Null inside them.
Anyone has any idea how can I do this?
This is exactly what the coalesce function is for:
SELECT COALESCE(RoleDuty1,RoleDuty2,RoleDuty3,RoleDuty4,RoleDuty5,RoleDuty6,RoleDuty7,'All null) as FIRST_NON_NULL
FROM aTable
I think you have to use a COALESCE with a bunch of CASE statements, like this:
SELECT COALESCE(rd1, rd2, rd3, rd4, rd5, rd6, rd7)
FROM
(SELECT
CASE WHEN roleduty1 IS NOT NULL THEN 'roleduty1' ELSE NULL END AS rd1,
CASE WHEN roleduty2 IS NOT NULL THEN 'roleduty2' ELSE NULL END AS rd2,
....
FROM t) sub
SELECT (
CASE FirstNullColumn
WHEN 'RoleDuty1' THEN 'None'
WHEN 'RoleDuty2' THEN 'RoleDuty1'
WHEN 'RoleDuty3' THEN 'RoleDuty2'
WHEN 'RoleDuty4' THEN 'RoleDuty3'
WHEN 'RoleDuty5' THEN 'RoleDuty4'
WHEN 'RoleDuty6' THEN 'RoleDuty5'
WHEN 'RoleDuty7' THEN 'RoleDuty6'
ELSE 'RoleDuty7'
END
) AS LastNonNullColumn
FROM (
SELECT (
CASE
WHEN RoleDuty1 IS NULL THEN 'RoleDuty1'
WHEN RoleDuty2 IS NULL THEN 'RoleDuty2'
WHEN RoleDuty3 IS NULL THEN 'RoleDuty3'
WHEN RoleDuty4 IS NULL THEN 'RoleDuty4'
WHEN RoleDuty5 IS NULL THEN 'RoleDuty5'
WHEN RoleDuty6 IS NULL THEN 'RoleDuty6'
WHEN RoleDuty7 IS NULL THEN 'RoleDuty7'
ELSE 'None'
END
) AS FirstNullColumn
FROM MyTable
)

If Else condition in mysql to check null

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

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 ...