使用 SQL Server,我想知道更新表中的列
code
所需的语法,其中包含另一列 customer
的前 x 个字符,然后是序列号。
所以我有一个保存客户记录的表。例如史密斯控股公司、史密斯有限公司、史密斯父子公司。 ETC…。我想用客户的前三个字符加上序列号填充该表 (
code
) 中的另一列 (customers
)。因此,三行将有一个新列,该新列的列设置为 SMI001,下一行列将在字段 SMI002 ETC 中有一个新条目……。 SMI003等等。
参见下面的示例。任何帮助将不胜感激。
customer code
----------------------------------------
Smith partners limited SMI001
Smith and sons SMI002
Smith plc SMI003
Smith and Co SMI004
SQL代码:
UPDATE customers
SET code = customer + 001
CREATE TABLE mytable(
customer VARCHAR(100) NOT NULL
);
INSERT INTO mytable
(customer ) VALUES
('Smith partners limited'),
('Smith and sons'),
('Smith plc'),
('Smith and Co');
添加栏目
ALTER TABLE mytable
ADD code VARCHAR(100);
一起使用,一起更新
with t as (
select customer,code,concat(upper(left(customer,3))
,'00',
row_number() over (order by (select null))) code1
from mytable
)
update t
set code=code1