[英]CASE statement IF ELSE in SQL Server
I have this select
statement and what I am trying to accomplish is to get data in the 我有这个
select
语句,我要完成的工作是在
DosagePerUnits
column only if Dosage
is not equal to empty or 1 and if Units
is not empty or 1. 仅在“
Dosage
不等于空或1且“ Units
不为空或1时,才显示DosagePerUnits
列。
Can someone help me please ? 有人能帮助我吗 ?
select
sbd.Code, c.Description, sbd.Dosage,
Case
when sbd.Units = '' then '1'
else sbd.Units
end as Units,
ad.ApptDate, sbd.RCycle, sbd.RWeek, sbd.RDay,
t.HistoryOrder, t.TypeId, sbd.Dosage + '/' + sbd.UnitsAS DosagePerUnits
from
bill_SuperBillDetail sbd,
bill_ProcedureVerification pv,
AppointmentData ad,
CPTCode c,
CPTType t
where
sbd.AccessionNumber = pv.AccessionNumber
and pv.ApptId = ad.ApptId
and ad.PatientId = 443
and ad.ApptDate <= GETDATE()
and ad.ApptDate > '2009-11-15 00:00:00.000'
and c.TypeId = t.TypeId
According to your description, it should be like this: 根据您的描述,应该是这样的:
CASE WHEN ISNULL(sbd.Dosage, '1') <> '1' AND ISNULL(sbd.Units, '1') <> '1' THEN
sbd.Dosage + '/' + sbd.Units
ELSE NULL END AS DosagePerUnits
ISNULL(x, 1)
replaces x with 1 if it is null. ISNULL(x, 1)
为null,则将其替换为1。 So ISNULL(x, 1)
is = 1 if x is either 1 or NULL. 因此
ISNULL(x, 1)
如果x为1或NULL ISNULL(x, 1)
= 1。
EDIT: Changed to the assumption that [Dosage] and [Unit] are both varchar as your comment indicates. 编辑:更改为[用法]和[单位]均为varchar的假设,如您的注释所示。
如果sbd.Units =''则为Case,而不是Case,否则sbd.Units以Units结尾,请尝试Coalesce(sbd.Units,1)作为Units,
If any part of the calculation is null the whole thing will be null. 如果计算的任何部分为空,则整个事物将为空。 You can use this fact this way:
您可以通过以下方式使用此事实:
nullif(nullif(sbd.Dosage, ''), '1') + '/' + nullif(nullif(sbd.Units, ''), '1') AS DosagePerUnits
This will result in DosagePerUnits being null when one of the columns are '', '1' or null 当列之一为'',``1''或null时,这将导致DosagePerUnits为null
I thought i had a really clever answer here. 我以为我在这里有一个非常聪明的答案。 Oh well, I guess it wasn't that clever.
哦,我想那不是那么聪明。 Does anyone know why I am unable to write comments ?
有谁知道我为什么不能写评论?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.