简体   繁体   English

如果是,则返回现有列中的值

[英]if then else to return value in existing column

In Access 2016 using SQL. 在Access 2016中使用SQL。 I need to add a new column called "Customer". 我需要添加一个名为“客户”的新列。 The value is based on if there is data in "Group Name" then use "Group Name". 该值基于“组名”中是否有数据,然后使用“组名”。 If "Group Name" is blank then use the data in "Description". 如果“组名”为空,则使用“描述”中的数据。

This is what I'm trying to use and it's not working. 这是我正在尝试使用的方法,但是没有用。

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

The solution has to be using SQL as once it works in Access it is used in another software. 该解决方案必须使用SQL,因为它一旦在Access中工作就可以在其他软件中使用。 I am pretty new to this so any help will be greatly appreciated. 我对此很陌生,因此将不胜感激。

The IsEmpty Function "Returns a Boolean value indicating whether a variable has been initialized." IsEmpty函数 “返回指示变量是否已初始化的布尔值”。

When you apply that function to your [Group name] field, it will always return False, which means this IIf() expression will always give you [Group name] ... 当您将该函数应用于[Group name]字段时,它将始终返回False,这意味着此IIf()表达式将始终为您提供[Group name] ...

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

You explained that you actually want IIf() to give you [Group name] when it is not Null, or [Description] otherwise. 您已经解释说,实际上,您希望IIf()为您提供[Group name]而不为Null的信息,否则为[Description] To do that, you can substitute IsNull() for IsEmpty() : 为此,您可以将IsNull()替换为IsEmpty()

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

Here are a couple other ways to accomplish the same thing: 以下是完成同一件事的其他几种方法:

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

Oops! 糟糕! You actually said "blank" , but I interpreted that to mean Null. 您实际上说的是“空白” ,但我将其解释为“空” If blank could also mean an empty string, use this to give you [Description] when [Group name] contains either Null or an empty string ... 如果空白也可能意味着一个空字符串,请在[Group name]包含Null或一个空字符串时使用它为您提供[Description]

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

In ANSI SQL this can be done in two update statements: 在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] = '';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM