N K N K - 5 months ago 14
SQL Question

Get text instead of ID

I have a query in which I am getting

Department_Id
as 812 its Integer value. I have referenced this ID with text in another table which is

select type_desc from type_mst_a where master_mkey = 812


and my query is

select convert(varchar(15),doc_Date,103)Doc_Dates,department_id,
case outward_Type when 'N' then 'None' when 'P' then 'Private' when 'C' then 'Confidential'
end [Type], convert(varchar(15),ref_date,103) Ref_dates, convert(varchar(15),Updated_Bill_Date,103)Updated_Bill_Dates ,
convert(varchar(15), Due_Date,103)Due_dates,* from view_A_Inward_Doc_Tracking_Hdr
where delete_flag='N' and mkey= 227381


How to get the value of that ID ?

IMG

Answer

You can use a sub-select to convert the department_id to a text as follows:

select convert(varchar(15), doc_Date, 103) Doc_Dates,
       (select type_desc from type_mst_a where master_mkey = department_id),
       ...

Alternatively, you could join the table type_mst_a.

select convert(varchar(15), doc_Date, 103) Doc_Dates,
       department_id,
       text.type_desc,
       case outward_Type when 'N' then 'None' when 'P' then 'Private' when 'C' then 'Confidential' end [Type],
       convert(varchar(15), ref_date, 103) Ref_dates,
       convert(varchar(15), Updated_Bill_Date,103) Updated_Bill_Dates,
       convert(varchar(15), Due_Date, 103) Due_dates,
       *
from   view_A_Inward_Doc_Tracking_Hdr INWARD
left outer join type_mst_a text on text.master_mkey = department_id
where  INWARD.delete_flag = 'N'
and    mkey = 227381

The outer join guarantees that - if no suitable text can be found - the result of the query does not vanish.