如何在SQL中更新多列?

时间:2010-12-15 09:48:53

标签: sql sql-server sql-server-2005 tsql

我的sql知识如此简单我需要你的帮助来编写sql update查询。我想更新where workorder=12300000 column1.TrimStart(B7CL).Addto(column2)。但是我怎么能在SQL Server 2005中这样做,请看下面:

转换此表

workorder |column1            | column2         
12300000  |B7CL-B32-41-00-5A  | NULL
12400000  |B7CL-B33-42-00-5A  | NULL
12300000  |B7CL-B34-44-03-5A  | NULL
12600000  |B7CL-B35-45-01-5A  | NULL
12300000  |B7CL-B36-45-01-5A  | B36-45-01-5A

到这个表

workorder |column1            | column2
12300000  |B7CL-B32-41-00-5A  | B32-41-00-5A
12400000  |B7CL-B33-42-00-5A  | B33-42-00-5A
12300000  |B7CL-B34-44-03-5A  | B34-44-03-5A
12600000  |B7CL-B35-45-01-5A  | B35-45-01-5A
12300000  |B7CL-B36-45-01-5A  | B36-45-01-5A

3 个答案:

答案 0 :(得分:3)

尝试类似:

UPDATE dbo.YourTable
SET column2 = SUBSTRING(column1, 6, 12)
WHERE column2 IS NULL

或者如果你想应用你提到的WHERE子句:

UPDATE dbo.YourTable
SET column2 = SUBSTRING(column1, 6, 12)
WHERE workorder = 12300000

但是,您的示例似乎不使用此WHERE子句.....

答案 1 :(得分:2)

这样的东西
UPDATE Table
SET Column2 = REPLACE(Column1,'B7CL-','')
WHERE Column2 ISNULL

或使用where子句

UPDATE Table 
SET Column2 = REPLACE(Column1,'B7CL-','') 
WHERE Column2 ISNULL
AND workorder=12300000

答案 2 :(得分:2)

 Update Table
    Set Column2 = Replace(Column1, 'B7CL-','')
    Where Column2 Is Null
    AND workorder=12300000