我有一个特定表的记录,其中包含字段:phone1
,phone2
。
如何在这些字段中将057
的值更改为053
,但只是值的开头?
例如:057-4353009
应更改为053-4353009
,但057-5405731
应更改为053-5405731
(此特定数字中的第二个057
不应更改)。
你必须实现两个部分:
WHERE phone LIKE '057%'
)057
之后的部分并与新的部分连接('053' + RIGHT(phone, LEN(phone) - 3)
)以下是执行此操作的示例查询:
UPDATE
tbl
SET
phone = '053' + RIGHT(phone, LEN(phone) - 3) -- Leaving the first 3 characters and use another one
WHERE
phone LIKE '057%' -- Starts with 057
一般的解决方案是这样的:
DECLARE
@SearchString NVARCHAR(MAX) = '057'
, @ReplaceString NVARCHAR(MAX) = '053'
UPDATE
tbl
SET
phone = @ReplaceString + RIGHT(phone, LEN(phone) - LEN(@SearchString))
WHERE
phone LIKE (@SearchString + '%')
试试这个(适合我):
UPDATE table1
SET phone = table2.leftc + table2.rightc
FROM table1 INNER JOIN
(select '053' as leftc, RIGHT(phone ,Len(phone) - 3)as rightc, phone
from table1
where LEFT(phone, 3) = '057') AS table2 ON table1.phone = table2.phone
对于sql server 2012使用的东西
update tbl
set phone_number=stuff(phone_number,1,3,'053')
WHERE
phone_number LIKE '057%'