Sql server 把一列一分为二?

Sql server 把一列一分为二?,sql-server,Sql Server,我在用这个 ALTER Table Analytics.dbo.[Parent Table] ADD [Cost Center] AS Left([Cost Center1], Charindex(':', [Cost Center1])-1) ALTER Table Analytics.dbo.[Parent Table] ADD [Project] as Substring([Cost Center1], Charindex(':', [Cost Center1])+1,

我在用这个

ALTER Table Analytics.dbo.[Parent Table]
ADD [Cost Center] AS Left([Cost Center1], Charindex(':', [Cost Center1])-1)

ALTER Table Analytics.dbo.[Parent Table]
ADD [Project] as Substring([Cost Center1], Charindex(':', [Cost Center1])+1,
              len([Cost Center1])-Len(Charindex(':', [Cost Center1])))

这很有效。但现在的问题是,CostCenter1列中有许多条目使用“-”而不是“:”。现在如何拆分整个列?

您可以使用下面给出的代码更新所有数据

UPDATE MyTable

SET columnName = REPLACE (columnName, '-', ':');
对成本中心1使用函数:

ALTER Table Analytics.dbo.[Parent Table]
ADD [Cost Center] AS Left([Cost Center1], Charindex(':', REPLACE([Cost Center1],'-',':'))-1)

ALTER Table Analytics.dbo.[Parent Table]
ADD [Project] as Substring([Cost Center1], Charindex(':', REPLACE([Cost Center1],'-',':'))+1,
              len([Cost Center1])-Len(Charindex(':', REPLACE([Cost Center1],'-',':'))))

因此
[Cost center 1]
可以同时具有
-
。用包含当前问题的数据显示示例数据和预期结果只需将“-”替换为“:”然后再使用“:”。因此,我有一些条目,如5000:gsasd、4534:fbgajf、35253 adsg、,324成本中心中的adsg 1我必须拆分数据,以便“:”和“-”之前的部分进入成本中心,数据在两者进入项目后进入项目。