替换字符串中的特定字符

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月26日 11:01:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定