英文:
Replacing only certain characters in a string
问题
Here's the translated code snippet to achieve your desired result:
SET Document_Number = REPLACE(Document_Number, 'CKS-', 'FIN-');
This code will replace the 'CKS-' format with 'FIN-', effectively changing the format of your Document_Number column.
英文:
I have the following column:
Document_Number |
---|
FIN-0582 |
FIN-7624 |
CKS-584202 |
CKS-891026 |
As can be seen above, there are two formats in this column: One beginning with 'FIN-' followed by 4 characters, while the other starts with 'CKS-' followed by 6 characters.
I need to change the 'CKS-' format into the same as 'FIN-', by taking only the last 4 characters from the right.
The expected result would look like this:
Document_Number |
---|
FIN-0582 |
FIN-7624 |
FIN-4202 |
FIN-1026 |
I have tried using the replace function, but I don't know what to do after this:
SET Document_Number = replace(Document_Number, 'CKS', 'FIN')
答案1
得分: 1
你想要最右边的4位数字... 使用 RIGHT
函数。
select
'FIN-' + right(Document_Number, 4) Document_Number
from (
values
('CKS-584202')
, ('FIN-7624')
) x (Document_Number);
返回结果:
Document_Number |
---|
FIN-4202 |
FIN-7624 |
英文:
You want the right most 4 digits... use the RIGHT
function.
select
'FIN-' + right(Document_Number, 4) Document_Number
from (
values
('CKS-584202')
, ('FIN-7624')
) x (Document_Number);
Returns
Document_Number |
---|
FIN-4202 |
FIN-7624 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论