if then else返回现有列中的值

时间:2016-05-27 15:39:11

标签: sql ms-access formula ms-access-2016

在Access 2016中使用SQL。我需要添加一个名为“Customer”的新列。该值基于“组名”中是否有数据,然后使用“组名”。如果“组名”为空,则使用“描述”中的数据。

这是我正在尝试使用它并没有用。

SELECT iIf(ISempty([group name])= FALSE,[Group name],[Description]) as Customer

解决方案必须使用SQL,因为它在Access中工作,它在另一个软件中使用。我是新手,所以任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

IsEmpty Function "返回一个布尔值,指示变量是否已初始化。"

当您将该功能应用于[Group name]字段时,它将始终返回False,这意味着此IIf()表达式始终会为您提供[Group name] ...

SELECT IIf(ISempty([group name])= FALSE,[Group name],[Description]) as Customer

您解释说,实际上您希望IIf()在{0}为空时给您[Group name],否则为[Description]。为此,您可以将IsNull()替换为IsEmpty()

SELECT IIf(IsNull([Group name])=False, [Group name], [Description]) As Customer

以下是完成同样事情的其他几种方法:

SELECT IIf([Group name] Is Null, [Description], [Group name]) As Customer
SELECT Nz([Group name], [Description]) As Customer

糟糕!你实际上说"空白" ,但我将其解释为Null。如果空白也可能表示空字符串,请在[Description]包含空字符串或空字符串时使用此字符串[Group name] ...

SELECT IIf(Len([Group name] & '') = 0, [Description], [Group name]) As Customer

答案 1 :(得分:0)

在ANSI SQL中,这可以在两个更新语句中完成:

-- First replace empty [Customer] with [Group Name]
Update [TargetTable] Set [Customer] = [Group Name]
Where [Customer] IS NULL OR [Customer] = '';

-- Then replace still empty [Customer] with [Description]
Update [TargetTable] Set [Customer] = [Description]
Where [Customer] IS NULL OR [Customer] = '';