How can i set a string values based on integer variable while executing sql query?

324 Views Asked by At

SQL Query

sessionFactory.getCurrentSession().createSQLQuery("select claim.encounterId, claim.claimUniqID, patientmaster.FirstName, tbl_insurance.insurance_name, claim.status from rcmdb.claim join rcmdb.encounter on claim.encounterID=encounter.encounterID join rcmdb.insurance_details on encounter.insuranceDetailsID=insurance_details.insuranceDetailsID 
join rcmdb.tbl_insurance on insurance_details.insurance=tbl_insurance.insurance_id 
join rcmdb.patientmaster onpatientmaster.patientMasterID=encounter.patientMasterID
where createdByDate between'"+from+"' and '"+to+"'").list();

i want to return string values based on claim.status values like if the status is 1 accepted, in output I want the string values how can I write the query?

2

There are 2 best solutions below

1
On

You can use CASE statement. https://www.w3schools.com/sql/func_mysql_case.asp

SELECT CASE
    WHEN status =1 THEN STRING
    ELSE NULL
END
0
On

Put a table in the database that maps the int to the string and join it:

ClaimStatus
--------------
ID, StatusDescription
1, Accepted
2, Rejected

SELECT c.PolicyNumber, c.ClaimValue, cs.StatusDescription
FROM
  claims c
  INNER JOIN claimstatus cs ON c.ClaimStatusId = cs.ID