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",
Related
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 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
I'm trying to change the values of 150 columns to the following;
'0 = Not provided'
' 1 = Yes '
' 2 = No '
I was able to do this using a case statement for each column. But the problem is it creates puts everything into one column. Is there a way to do it for each individual column without writing 150 case statements? The columns need to be in a specific order.
example:
SELECT CASE
WHEN Answer.Question1_ID is null THEN 'Not Provided'
WHEN Answer.Question1_ID = 1 THEN 'Yes'
WHEN Answer.Question1_ID = 2 Then 'No'
End as 'Question1',
CASE
WHEN Answer.Question2_ID is null THEN 'Not Provided'
WHEN Answer.Question2_ID = 1 THEN 'Yes'
WHEN Answer.Question2_ID = 2 Then 'No'
End as 'Question2'
.
.
.
From Answer
Is there a way to do it for each individual column without writing 150 case statements?
No.
You can use a program to write the case statements if need be.
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)
declare #SP nvarchar(3)
SELECT HDR.SOPNUMBE, HDR.docdate, HDR.pymtrmid,
RTRIM(HDR.ShipToName) +
CASE WHEN HDR.ADDRESS1 <> '' THEN char(10) + RTRIM(HDR.ADDRESS1) ELSE '' END
FROM vSOPHdr HDR
WHERE HDR.SOPTYPE = 2 AND
(CASE WHEN HDR.SLPRSNID <> 'All'
then HDR.SLPRSNID in (''+#SP+'')
else HDR.SLPRSNID between '0' and 'ZZZZZZZZZZZZZZZ'
end)
I am trying to run the above query in sql query analyzer but getting 'syntax' errors in the CASE expression (near IN, ELSE). What could be wrong?
All I want to do is when sales-personID is selected as ALL in the report parameter I would like to process all sales person else only the selected sales person ID.
Just a background about what I am doing,
I have a similar query to the above query (without the CASE condition) within a dataset for a SSRS report.
Note that #SP is a report parameter.
CASE is an expression that returns a single value. It cannot be used for control-of-flow logic, like in other languages such as VB. Try:
WHERE HDR.SOPTYPE = 2 AND
((HDR.SLPRSNID <> 'All' AND HDR.SLPRSNID = #SP)
OR
(HDR.SLPRSNID = 'All'))
Though it seems like maybe you need instead, which would make more logical sense if the user is potentially passing 'All' into the parameter:
WHERE HDR.SOPTYPE = 2 AND
((#SP <> 'All' AND HDR.SLPRSNID = #SP)
OR
(#SP = 'All'))
you can just use an OR clause
WHERE HDR.SOPTYPE = 2 AND
((HDR.SLPRSNID <> 'All' AND HDR.SLPRSNID IN (''+#SP+''))
OR
(HDR.SLPRSNID = 'All' AND HDR.SLPRSNID BETWEEN '0' and 'ZZZZZZZZZZ'))
Not sure if the BETWEEN '0' and 'ZZZZZZZZZZZ' means "IN everything". If yes, you can just remove it now.
If you absolutely wanna use a CASE WHEN, you can do it this way :
WHERE HDR.SOPTYPE =2
AND (CASE WHEN HDR.SLPRSNID <>'All' AND HRD.SLPRSNDID IN (''+#SP+'')) THEN 1
WHEN HDR.SLPRSNID = 'All' AND HDR.SLPRSNID BETWEEN '0' and 'ZZZZZZZZZZ' THEN 1
ELSE 0
END) = 1