SQL查询在条件表中添加新列的新列

时间:2021-08-02 07:45:43

I want to add the new column with the condition that if the value of 1st columns is diff. then 2nd columns then only it will be alter with condition OR else the value remain same.

我想添加新列,条件是如果第1列的值是diff。然后是第二列,然后只有条件OR才会改变,否则值保持不变。

SQL查询在条件表中添加新列的新列

The Above image will clarify my requirement more effectively.

上面的图片将更有效地阐明我的要求。

Below query only alter the column but please guide how to apply condition in the same.

以下查询仅更改列,但请指导如何应用条件。

ALTER TABLE Wealth_CFY ADD Type3 AS (Type1 + ' ' + Type2)

2 个解决方案

#1


2  

Use a case statement to check if the values are same before concatenating.

在连接之前使用case语句检查值是否相同。

ALTER TABLE Wealth_CFY
  ADD Type3 AS (CASE WHEN COALESCE(Type1, '') <> COALESCE(Type2, '') THEN Type1 + ' ' + Type2 ELSE Type1 END) PERSISTED

COALESCE is used to handle NULL values. If both Type1 and Type2 does not accept NULL value then remove the COALESCE function

COALESCE用于处理NULL值。如果Type1和Type2都不接受NULL值,则删除COALESCE函数

#2


1  

 ALTER TABLE Wealth_CFY
  ADD Type3 AS (CASE WHEN COALESCE(Type1, '') <> COALESCE(Type2, '') THEN 
   Type1 + ' ' + Type2 ELSE Type1 END) PERSISTED

#1


2  

Use a case statement to check if the values are same before concatenating.

在连接之前使用case语句检查值是否相同。

ALTER TABLE Wealth_CFY
  ADD Type3 AS (CASE WHEN COALESCE(Type1, '') <> COALESCE(Type2, '') THEN Type1 + ' ' + Type2 ELSE Type1 END) PERSISTED

COALESCE is used to handle NULL values. If both Type1 and Type2 does not accept NULL value then remove the COALESCE function

COALESCE用于处理NULL值。如果Type1和Type2都不接受NULL值,则删除COALESCE函数

#2


1  

 ALTER TABLE Wealth_CFY
  ADD Type3 AS (CASE WHEN COALESCE(Type1, '') <> COALESCE(Type2, '') THEN 
   Type1 + ' ' + Type2 ELSE Type1 END) PERSISTED