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)
Related
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`
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
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
)
I have an existing Stored Procedure and i add some parameter to filter the result.But when #id_account is null it returns Empty Data. I want to skip AND when #id_account is null, i try IF condition but it wont work. Any suggestion? Thanks :D
AND
(
pd_account.id_account = #id_account
)
Put in the condition you really want, which is:
(pd_account.id_account = #id_account or #id_account is null)
Use:
AND
(
pd_account.id_account = COALESCE(#id_account,pd_account.id_account)
)
From COALESCE doc
Returns the first non-NULL value in the list, or NULL if there are no
non-NULL values.
So when #id_account is not provided the condition will be pd_account.id_account =pd_account.id_account which is equivalent to 1=1 always true.
You can use CASE like:
AND pd_account.id_account = CASE WHEN #id_account IS NOT NULL THEN #id_account
ELSE pd_account.id_account
END
I'm migrating a report from Crystal where i was able to create a column and fill using this formula...
if ({#SD Profile}="null") and ({#HD Profile 2}="null")
then ""
else
if ({#HD Profile 2}="null")
then "SD only"
else
if ({#SD Profile}="null")
then "HD only"
else
if({#SD Companion}="1")
then "HD+SD"
else "HD/SD"
I'd like to accomplish the same thing using SQL where i populate the values of a column based on the data in 3 other columns. Note I'm not adding a column to the DB just inserting into the report.
I've created the empty column in my SELECT statement using
CAST(NULL AS VARCHAR(30)) as "Content Type",
"SD_format"."name" as "SD Format",
"HD_format"."name" as "HD Format",
"destination"."sd_companion_required_flag"
Basically something like... If SD_Format.name ='null' then Content_Type = '' etc.
You should be able to replace the 'if' statement by a 'case' statement:
select
case
when SD_Profile is null and HD_Profile is null then ''
when HD_Profile is null then 'SD only'
when SD_Profile is null then 'HD only'
when Destination = '1' then 'HD + SD'
else 'HD/SD'
end as 'Content_Type'
from ...
*"SD_format"."name" as "SD Format",
"HD_format"."name" as "HD Format",
case
when "SD_format"."name" is null and "HD_format"."name" is null then ''
when "HD_format"."name" is null then 'SD only'
when "SD_format"."name" is null then 'HD only'
when "destination"."sd_companion_required_flag" = '1' then 'HD + SD'
else 'HD/SD'
end as "Content_Type",