How to select last column without Null in mySQL - 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
)

Related

Concatenate only when all values are true in mysql

The following is the query I have used to concatenate siretagprefix and siretagsec
CONCAT('TZN',
`adggtnz`.`reg04_rpt_animreg`.`siretagprefix`,
`adggtnz`.`reg04_rpt_animreg`.`siretagsec`) AS `siretagid`,
The result of the query when siretagprefix and siretagsec when the values are null, I get TZN as the result. How can I concatenate only when all the values are true.
You could use CASE to check your column values
CASE WHEN `adggtnz`.`reg04_rpt_animreg`.`siretagprefix` IS NOT NULL
AND `adggtnz`.`reg04_rpt_animreg`.`siretagsec` IS NOT NULL
THEN
CONCAT('TZN',
`adggtnz`.`reg04_rpt_animreg`.`siretagprefix`,
`adggtnz`.`reg04_rpt_animreg`.`siretagsec`)
ELSE NULL END AS siretagid
You could use a case expression:
CASE WHEN `adggtnz`.`reg04_rpt_animreg`.`siretagprefix` IS NOT NULL AND
`adggtnz`.`reg04_rpt_animreg`.`siretagsec` IS NOT NULL
THEN CONCAT('TZN',
`adggtnz`.`reg04_rpt_animreg`.`siretagprefix`,
`adggtnz`.`reg04_rpt_animreg`.`siretagsec`)
END AS `siretagid`

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

Issue with NULL value in nested case

Long story short i need to return a varchar value if stored value is null but eithers bring me a NULL value or varchar value here is the code.
in the table i have some fields null and some with data
CASE WHEN DS.DBIRTH+DS.MBIRTH+DS.YBIRTH =
'' THEN CONVERT(VARCHAR(10),PT.CLIENTDBIRTHDATE,111)
ELSE 'INPUT DATE OF BIRTH'
CASE WHEN PT.CLIENTBIRTHDATE IS NULL THEN ''
ELSE '1800-01-01'
END
END AS BFIELD
this brings me something like this
NULL
1917/05/02
NULL
1923/02/02
1967/01/05
NULL
but i need something like this
01/01/1800
1917/05/02
01/01/1800
1923/02/02
1967/01/05
01/01/1800
sorry for the ultra noob question
Your current case statement looks off. I imagine what you want to do is wrap the result in coalesce:
COALESCE(CASE
WHEN DS.DBIRTH+DS.MBIRTH+DS.YBIRTH = ''
THEN CONVERT(VARCHAR(10),PT.CLIENTDBIRTHDATE,111)
ELSE 'INPUT DATE OF BIRTH'
END, '01/01/1800') AS BFIELD
Without the Case statement it should be something as simple as....
select CONVERT(VARCHAR(10) ,
CAST(ISNULL(DateColumn,'01/01/1800') AS DATE)
,111)

NULL != NULL in mysql query

I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:
SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )
And it only returns results where the column is not NULL. Why is this so, and how do I get around it?
Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).
You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.
Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.
That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null
Try this to illustrate the situation.
SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1
If you rewrite your query like below, your problems with ignoring NULLs will go away:
SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'
I think you can use
(table.Field = table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)

MySQL Left Join, SELECT a field either from one or the other if null

I am trying to LEFT JOIN 2 tables. which is working out fine. But i am getting back two sets of fields named setting_value. iam trying to get tblSettings.setting_value only if tblAgencySettings.setting_value is NULL. How would i go about doing this? I know i can rename the fields, then in PHP i can check the tblAgencySettings.setting_value and if NULL then grab the tblSettings.setting_value but i prefer to keep this at MySQL.
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
`tblSettings`.`setting_value`, `tblAgencySettings`.`setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
slight issue i just noticed. i failed to mention this. if tblAgencySettings.setting_value does have a value. but changeable is not 1 then just select tblSettings.setting_value
Just add a COALESCE:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
The COALESCE function returns the first non-NULL value you give it so this:
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
Will be tblAgencySettings.setting_value if that's not NULL and tblSettings.setting_value if tblAgencySettings.setting_value is NULL.
If tblAgencySettings.setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above:
COALESCE(
IF(`tblAgencySettings`.`setting_value` = 0, NULL, `tblAgencySettings`.`setting_value`),
`tblSettings`.`setting_value`
)
The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL. Or, you could go all the way to a CASE statement:
case
when `tblAgencySettings`.`setting_value` = 0 then `tblSettings`.`setting_value`
when `tblAgencySettings`.`setting_value` IS NULL then `tblSettings`.`setting_value`
else `tblSettings`.`setting_value`
end
Change your SQL Statement to this:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
CASE WHEN `tblSettings`.`setting_value` IS NULL THEN `tblAgencySettings`.`setting_value`
ELSE `tblSettings`.`setting_value` END AS `setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
Here's a link to MYSQL CASE Statement for your reference.