0

我有一个派生列,我从一个字段“BANK_REF”生成,我想从中删除任何破折号和空格,然后用前导 0 填充它,最多 10 个字符。以下处理删除破折号和空格:

REPLACE(TRIM(BANK_REF), "-","")

以下添加是前导 0:

(DT_WSTR,6)REPLICATE("0",6 - LEN(BANK_REF)) + BANK_REF

结合我得到的两者:

(DT_WSTR,6)REPLICATE("0",6 - LEN(REPLACE(TRIM(BANK_REF), "-",""))) + REPLACE(TRIM(BANK_REF), "-","")

这实现了我所追求的,但是它似乎效率低下,因为我复制了替换/修剪部分。有没有更好的方法来实现这一目标?

4

1 回答 1

0

You can do it this way:

select right(replicate("0", 10)+REPLACE(TRIM(BANK_REF), "-",""), 10)

I'm not sure if you will notice the difference in efficiency. Generally SQL queries are dominated by I/O rather than in-memory operations.

于 2013-04-03T15:54:57.830 回答