使用 RIGHT 从列中获取数据并更新以将结果添加到新列

using RIGHT to get data from column and update to add result to new column

我很难做对。 我通过此查询获取数据

Select RIGHT(RTRIM(Nimi), 3) Riik from TABLE

但是当我在更新中使用它时,它有不止一个结果要插入。

所以当我这样使用它时:

update #temp_table 
set veerg2 = (Select RIGHT(RTRIM(nimi), 3) nimi2 
from #temp_table a 
where a.nimi is not NULL )

然后我收到错误,但我需要将这最后三个字符放入新列中。

需要帮助。

你的语法看起来像 SQL 服务器。正确的查询最有可能是:

update a
    set veerg2 = RIGHT(RTRIM(nimi), 3)  
    from #temp_table a 
    where a.nimi is not NULL ;

两个变化是:

  • from 子句中定义的 table 别名 用于 update 而不是 table 名称。
  • select 已从 set 中删除。没必要。

你也可以不用 from:

update #temp_table
    set veerg2 = RIGHT(RTRIM(nimi), 3)  
    where nimi is not NULL ;