T-SQL/SQL- 根据条件将值从视图中的一列复制到另一列
T-SQL/SQL- Copy Value From One Column to Another in View Depending on Condition
我有问题。我将尝试在 Northwind 数据库上展示它。
https://northwinddatabase.codeplex.com
我想查看 "Customer" table,其中 "Region" 列的别名是 "Country",但我的值来自 [=24] =] 列在 "Country" 列中。我用 "ContactName" 列显示它。
我是这样做的:
CREATE VIEW test AS
SELECT ContactName, Region AS Country FROM Customers
现在我想修改一下。我想 "copy" 值从 "Region" 到 "Country" 如果它不是 null/not 空。如果是的话,我想保留国家的价值。
我不知道是否可以查看并使用别名和大小写。
您正在寻找 ISNULL
或 COALESCE
。 ISNULL
将查看第一个参数,如果它为 NULL,则转到第二个参数。
SELECT ContactName, ISNULL(Region,Country) AS Country FROM Customers
我有问题。我将尝试在 Northwind 数据库上展示它。 https://northwinddatabase.codeplex.com
我想查看 "Customer" table,其中 "Region" 列的别名是 "Country",但我的值来自 [=24] =] 列在 "Country" 列中。我用 "ContactName" 列显示它。
我是这样做的:
CREATE VIEW test AS
SELECT ContactName, Region AS Country FROM Customers
现在我想修改一下。我想 "copy" 值从 "Region" 到 "Country" 如果它不是 null/not 空。如果是的话,我想保留国家的价值。
我不知道是否可以查看并使用别名和大小写。
您正在寻找 ISNULL
或 COALESCE
。 ISNULL
将查看第一个参数,如果它为 NULL,则转到第二个参数。
SELECT ContactName, ISNULL(Region,Country) AS Country FROM Customers