I am having an issue using case/cast properly to convert a number into a CHAR. I've look on stack overflow and keep seeing different techniques, but none seem to work for me. I am sure I am missing something simple, but this will be a good learning experience for me.
SELECT c.Client_ID,
c.Last_Name,
c.First_Name,
d.Sched_Time,
d.Time_In,
d.Time_Out,
(Round(d.Sched_Duration/15,1)) AS Units,
(d.Time_Out - d.Time_In) * 60 * 24 AS Mins,
CASE
WHEN d.Eventstatus = 1 THEN CAST('Completed' AS char(20))
WHEN d.Eventstatus = 10 THEN CAST('Cancelled: No Show' AS char(20))
WHEN d.Eventstatus = 11 THEN CAST('Cancelled: Early Dismissal' AS char(20))
WHEN d.Eventstatus = 12 THEN CAST('Cancelled: Office Closed' AS char(20))
WHEN d.Eventstatus = 13 THEN CAST('Deleted - Error' AS char(20))
WHEN d.Eventstatus = 14 THEN CAST('Cancelled: Provider Absent' AS char(20))
WHEN d.Eventstatus = 15 THEN CAST('Cancelled: Waiver' AS char(20))
WHEN d.Eventstatus = 2 THEN CAST('Cancelled: Client Cancelled' AS char(20))
WHEN d.Eventstatus = 3 THEN CAST('Pending Completion' AS char(20))
WHEN d.Eventstatus = 4 THEN CAST('Cancelled: Absent' AS char(20))
WHEN d.Eventstatus = 5 THEN CAST('Cancelled: Out Of Medication' AS char(20))
WHEN d.Eventstatus = 6 THEN CAST('Cancelled: Other' AS char(20))
WHEN d.Eventstatus = 7 THEN CAST('Cancelled: Field Trip' AS char(20))
WHEN d.Eventstatus = 8 THEN CAST('Cancelled: Refused Treatment' AS char(20))
ELSE d.Eventstatus
END AS Appt_Status,
CASE
WHEN d.Activity_Desc = '<b>OASAS</b> <i>(Group)</i>' THEN 'OASAS -Group'
WHEN d.Activity_Desc = '<b>OASAS</b> <i>(Individual)</i>' THEN 'OASAS - Individual'
ELSE d.Activity_Desc
END AS Visit_Reason,
CASE
WHEN d.ProvidersID = 113706 THEN CAST('Name One' AS char(20))
WHEN d.ProvidersID = 117917 THEN CAST('Name Two' AS char(20))
WHEN d.ProvidersID = 117919 THEN CAST('Name Three' AS char(20))
WHEN d.ProvidersID = 117920 THEN CAST('Name Four' AS char(20))
WHEN d.ProvidersID = 117921 THEN CAST('Name Five' AS char(20))
WHEN d.ProvidersID = 117922 THEN CAST('Name Six' AS char(20))
ELSE d.ProvidersID
END AS Provider,
d.SRVID,
d.dlsequence
FROM Clients c
INNER JOIN DAILY_LOG_DATA d
ON c.SHISID = d.SHISID
WHERE d.SRVID IN (37913, 36186, 36185, 36180, 36179, 36168, 36167, 36182, 36181, 36173, 36172, 36177, 36176, 36175, 36174, 36178, 36184, 36183, 36188, 36187)
AND d.Sched_Time >= TO_DATE('2020-09-7 01:00:00', 'YYYY/MM/DD HH:MI:SS')
AND d.Sched_Time < TO_DATE('2020-09-16 12:59:00', 'YYYY/MM/DD HH:MI:SS')
Don't cast the literal strings to char()
; they are strings already, and doint so does not address the actual issue, which is that all branches of a case
expression must return a value of the same datatype. Instead, cast the number to a string in the else
branch of the case
.
CASE
WHEN d.Eventstatus = 1 THEN 'Completed'
WHEN d.Eventstatus = 10 THEN 'Cancelled: No Show'
-- more "WHEN" branches here
WHEN d.Eventstatus = 8 THEN 'Cancelled: Refused Treatment'
ELSE TO_CHAR(d.Eventstatus)
END AS Appt_Status,
In Oracle, you could also use decode()
:
DECODE(d.Eventstatus,
1, 'Completed',
10, 'Cancelled: No Show',
-- ...
8, 'Cancelled: Refused Treatment',
TO_CHAR(d.Eventstatus)
) END AS Appt_Status
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.