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 ?

4
  • What version of Postgresql are you using? I'm able to get a similar query working on version 9.3 (sqlfiddle.com/#!15/7f940/4) Commented Apr 19, 2023 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? Commented Apr 19, 2023 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. Commented Apr 20, 2023 at 2:53
  • Can either d.DischargeStatusId or dr.DischargeRequestStatusId be NULL? What should the result be if neither of the two conditions are true? Commented Apr 20, 2023 at 3:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.