最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - How to update column values following a pattern? - Stack Overflow

programmeradmin6浏览0评论

I have a table's column, with values that need update format from 'A-XXXXX' to A-12-XXXXX' where X is an integer.

I'm using an update like this:

update table_name 
set column = 'A-[0-9][0-9][0-9][0-9][0-9]' 
where column = 'A-12-[0-9][0-9][0-9][0-9][0-9]'; 

but it is not working.

Can anyone make a suggestion on how to do this?

I have a table's column, with values that need update format from 'A-XXXXX' to A-12-XXXXX' where X is an integer.

I'm using an update like this:

update table_name 
set column = 'A-[0-9][0-9][0-9][0-9][0-9]' 
where column = 'A-12-[0-9][0-9][0-9][0-9][0-9]'; 

but it is not working.

Can anyone make a suggestion on how to do this?

Share Improve this question edited Jan 20 at 15:35 Thom A 95.6k11 gold badges60 silver badges92 bronze badges asked Jan 20 at 0:49 mariamaria 73 bronze badges 2
  • 1 just replace “A-” with “A-12-”? – BenderBoy Commented Jan 20 at 1:27
  • Please tag the database engine yo are using – DarkBee Commented Jan 20 at 6:43
Add a comment  | 

3 Answers 3

Reset to default 0

This should work. But I haven't tested it yet.

update table_name 
set column = REPLACE(column, 'A-', 'A-12-') 
where column LIKE 'A-%'; 

You could do it like this:

update table_name
set column =  STUFF(column,3, 0, '12-')
where column LIKE 'A-[0-9][0-9][0-9][0-9][0-9]';

In GoogleSQL you could do something like this:

UPDATE table_name
SET column = REPLACE(column, 'A-', 'A-12-')
WHERE REGEXP_CONTAINS(column, r'A-[0-9]{5}')
发布评论

评论列表(0)

  1. 暂无评论