return string value from SQL Server tables where column type is integer - sql

In my mssql tables i have a clomun that data type is integer. and when i was try thats you know the return value is int.
select h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi from hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
int this sql statement "h.durum"' data type is integer.
but i want to make thats.
when h.durum is 0 return string is Yönetici
when h.durum 1 string is Normal Kullanıcı
how can i do in sql?
thank you for patience

Not sure if I understood the question, but try replacing h.durum on your SELECT with the following:
CASE WHEN h.durum = 0 THEN 'Yönetici'
WHEN h.durum = 1 THEN 'Normal Kullanıcı'
ELSE '???' END AS some_column_alias

Try this:
select
CASE
WHEN h.durum = 0 THEN N'Yönetici'
WHEN h.durum = 1 THEN N'Normal Kullanıcı'
END CASE ColumnName,
h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi
FROM hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
;

Related

Using a CASE WHEN with multiple Whens that is only resulting in a BOOLEAN value

I am working on writing a SQL Query which is using a CASE line with multiple When's, and I was wondering why my result is only showing BOOLEAN (True/False) Values. Ideally I would like it to show whatever is after the THEN's in my code.
--with loan as (
SELECT DISTINCT
rtw.identifierdimsk
,lmmf.milestonesk
,lmd.groupname
,rtw.typecode
,rtw.campaignname
,rtw.partnername
,rtw.sourcename
,lmmf.date
,rtw.typecode = CASE WHEN rtw.typecode = 'PROS' THEN 'Prospect'
WHEN rtw.typecode = 'CARI' THEN 'Deluxe'
WHEN rtw.typecode = ('CARIC') OR rtw.typecode = ('CTE') THEN 'CSignal'
WHEN rtw.typecode = 'RMT' THEN 'Pre Pull'
WHEN rtw.typecode in ('90','90PLUS','C90','C90PLUS','BP','ELO','AUTO','POP') THEN 'In-process'
WHEN rtw.typecode = 'XU3%' THEN 'U3'
WHEN rtw.typecode = '%CT%' THEN 'Misc'
END AS Value
FROM rtal_winner rtw
INNER JOIN one_fact lmmf ON lmmf.identifierdimsk = rtw.identifierdimsk
INNER JOIN one_dim lmd ON lmd.milestonesk = lmmf.milestonesk
AND lmmf.date >= '2022-10-25'
Because you are comparing rtw.typecode to the result of the case expression:
rtw.typecode = CASE ...
remove the comparison part:
, lmmf.date
, CASE
WHEN rtw.typecode = 'PROS' THEN 'Prospect'
-- ...
END AS Value
FROM rtal_winner rtw

I have A problem with dividing the value of 0 by 0 in an Access database

When I try to extract the result of Division 2 Field from table in access database
If I have a value of 0 an error occurs
sqlSTR = "SELECT TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY AS ['Price']FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)
If you are trying to avoid the error, you can use a CASE statement in your query to check for a 0 value in the Item_Org_Price2 field and return a different value if it is 0. For example, you could do something like this:
SELECT
CASE
WHEN TBL_Category_Item_File.Item_Org_Price2 = 0
THEN 0
ELSE TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY
END AS ['Price']
FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)

How to select twice the same column on join query?

How Can I to count the gender when is M or F, somehting like
SELECT count(N.gender)
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1 and N.gender="M"
SELECT countif(N.gender = 'M') as M, countif(N.gender = 'F') as F
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1
Since you did not describe the structure of your table and use Spanish-looking identifiers, I will use a clearer example with my own schema:
SELECT
SUM( CASE WHEN Sex = 'M' THEN 1 ELSE 0 END ) AS M,
SUM( CASE WHEN Sex = 'F' THEN 1 ELSE 0 END ) AS F
FROM People
WHERE People.Dept = 5

Convert to linq sql query error

I have a query according to the :
SELECT
tblDefinition.TopsisType, tblTemplate.tType, tblEvaluationFormCategory.Title AS EvaluationCategory,
CASE tType WHEN 1 THEN N'2'
ELSE N'1' END ProcessType,
tblDefinition.Description, tblDefinition.TemplateID, 1 Destinition
FROM tblTemplate INNER JOIN
tblEvaluationFormCategory ON tblTemplate.CategoryID = tblEvaluationFormCategory.ID INNER JOIN
tblEFQMAlternative INNER JOIN
tblDefinition ON ([tblEFQMAlternative].[TopsisID] = [tblDefinition].[TopsisID] AND [tblEFQMAlternative].[CustomerID] = [tblDefinition].[CustomerID]) INNER JOIN
tblGroupMembers ON ([tblDefinition].[TopsisID] = [tblGroupMembers].[TopsisID]
AND tblDefinition.CustomerID =
tblGroupMembers.CustomerID) ON tblTemplate.TemplateID = tblDefinition.TemplateID
INNER JOIN
tblTerm ON tblDefinition.TermGrant = tblTerm.Title
WHERE (tblTerm.ID = 20) AND (tblGroupMembers.UserID = 558)
GROUP BY tblDefinition.TopsisType, tblTemplate.tType, tblEvaluationFormCategory.Title,
tblDefinition.Description, tblDefinition.TemplateID
when i convert to linq, then error to :
Field [([tblDefinition].[TopsisID] = [tblGroupMembers]].[[TopsisID] AND tblDefinition.CustomerID = tblGroupMembers.CustomerID) ON tblTemplate.TemplateID] not found in the current Data Context.
What should I do?

Is there a way to ignore the AND in a CASE when something is true?

I have this Where clause
Select * From Student_Info si
Inner Join Certifications cc
Inner Join Cert_Earned ce
Where si.grad_date = #grad_date
AND cc.org_no = #org_no
but I need an additional AND that should be ignored if it turns out the value is false, I will want ALL certificates
AND cc.industrial = CASE WHEN #industrial = 0 THEN Do Nothing
Else #industrial
This would normally be expressed as:
AND (#industrial = 0 OR ccc.industrial = #industrial)
It sounds like you just want to add a predicate that does an OR between two different conditions
AND (#industrial = 0 or ccc.industrial = #industrial)
You can do something like this with CASE functions:
AND 1 = (CASE WHEN #industrial = 0 THEN 1
ELSE CASE WHEN cc.industrial = #industrial THEN 1 END
END)
or if cc.industrial is not nullable, then maybe:
AND cc.industrial = CASE WHEN #industrial = 0 THEN cc.industrial
Else #industrial END