0

Full Query:

SELECT
        '' as [id],
        a.OrganizationId as [organization_id],
        a.AdmissionNo as [admission_no],
        a.AdmissionDate as [admission_date],
        p.MrNo as [mr_no],
        p.Name as [name],
        p.BirthDate as [birth_date],
        dr.RequestDate as [request_date],
        dr.DischargeRequestStatusId as [request_status_id],
        drs.Name as [request_status],
        a.DischargeDate as [discharge_date],
        dt.Name as [discharge_type],
        d.DischargeConditionId as [discharge_condition_id],
        dc.Name as [discharge_condition],
        d.Notes as [notes],
        as2.Name as [admission_status],
        u.Name as [create_user],
        '' as [created_at],
        '' as [updated_at],
        '' as [deleted_at],
        ( 
            CASE
                WHEN d.DischargeStatusId = 1 THEN 'true'
                WHEN dr.DischargeRequestStatusId = 1 THEN 'false'
            END
        ) as [is_discharge_status],
        a.AdmissionId as [admission_id],
        a.DischargeId as [discharge_id],
        p.NationalIdNo as [nationality_id_no]
    FROM 
        [HIS].PatientTrx.Admission a
    LEFT JOIN
        [HIS].Patient.Patient p ON a.PatientId = p.PatientId 
    LEFT JOIN 
        [HIS].PatientTrx.DischargeRequest dr ON a.DischargeRequestId = dr.DischargeRequestId
    LEFT JOIN 
        [HIS].Patient.DischargeRequestStatus drs ON dr.DischargeRequestStatusId = drs.DischargeRequestStatusId 
    LEFT JOIN 
        [HIS].PatientTrx.Discharge d ON a.DischargeId = d.DischargeId 
    LEFT JOIN 
        [HIS].Patient.DischargeType dt ON d.DischargeTypeId = dt.DischargeTypeId 
    LEFT JOIN 
        [HIS].Patient.DischargeCondition dc ON d.DischargeConditionId = dc.DischargeConditionId 
    LEFT JOIN 
        [HIS].Patient.AdmissionStatus as2 ON a.AdmissionStatusId = as2.AdmissionStatusId 
    LEFT JOIN 
        [HIS].[Security].[User] u ON d.CreateUserId = u.CreateUserId

I want the end result of the above query especially in CASE WHEN it generates BOOLEAN instead of STRING. How to Do It ?

CASE
    WHEN d.DischargeStatusId = 1 THEN true
    WHEN dr.DischargeRequestStatusId = 1 THEN false
END

if I write a query like this above it will get a SQL Error error [207] [S0001]: Invalid column name 'true'. How to get BOOLEAN results ?

My August
  • 29
  • 2
  • 6
  • What version of Postgresql are you using? I'm able to get a similar query working on version 9.3 (http://sqlfiddle.com/#!15/7f940/4) – Mike Apr 19 '23 at 19:59
  • 1) `WHEN d.DischargeStatusId = 1 THEN 'true'::boolean`. 2) I don't see this `as [is_discharge_status]` working. Not sure where you got the square bracket syntax from? – Adrian Klaver Apr 19 '23 at 20:02
  • The use of brackets around identifiers leads me to think that either the OP has prior experience with, or the code is intended for, SQL Server instead of PostgreSQL. – JohnH Apr 20 '23 at 02:53
  • Can either _d.DischargeStatusId_ or _dr.DischargeRequestStatusId_ be `NULL`? What should the result be if neither of the two conditions are true? – JohnH Apr 20 '23 at 03:20

0 Answers0