假设我有一个列名Address
。此列中存在的记录类似于ABC,XYZ,TX
编写Texas
而不是TX
的查询是什么?
这是你想要的吗?
select (case when address like '% TX'
then concat(left(address, length(address) - 2), 'Texas')
else address
end)
也就是说,您应该将状态存储在单独的列中。
如果你想更新你可以在更新中使用arepalce的值
update Your_Table
set Address = replace(Address, 'TX', 'Texas')
where Address like '%TX%'
或者最后的比赛
update Your_Table
set Address = replace(Address, 'TX', 'Texas')
where Address like '%TX'
或者如果您只需要一个选择
select replace(Address, 'TX', 'Texas') address
from Your_Table
where Address like '%TX%'
TX
可以在字符串的任何部分,你不需要像这样的字符串:
ABCTXD,XYZ,TX
第一次发生TX被替换,对吧?
因此,要覆盖所有情况,您需要将,
连接到字符串的开头和结尾,然后进行替换:
update tablename
set address = trim(',' FROM replace(
concat(',', address, ','),
concat(',','TX', ','),
concat(',', 'Texas', ',')
))
where concat(',', address, ',') like concat('%,', 'TX', ',%')
见demo。
据我所知,你要重写每张TX
,在你的桌子的Adress
栏中,由Texas
重写。这是实现该目标的SQL查询。
UPDATE TableName
SET Address = replace(Address , 'TX', 'Texas')
WHERE Address LIKE '%TX'