Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

sql server - How to Replace all occurrence of a character except last in a string in SSIS?

I have a string number like this:

1.000.000.00

So I have to delete all dots except last one, it will look like this:

1000000.00

Also if there is only one dot it should keep it.

Example:

INPUT            |       OUTPUT
-----------------------------------
1.00             |       1.00
1.000.00         |       1000.00

What I tried:

I create a derived column with this expression

LEFT([Column],FINDSTRING([Column],".",LEN([Column]) - LEN(REPLACE([Column],".",""))) - 1) 
+ SUBSTRING([Column],FINDSTRING([Column],".",LEN([Column]) - LEN(REPLACE([Column],".",""))) + 1,LEN([Column]))

It keep the first dot not the last one.

question from:https://stackoverflow.com/questions/65893761/how-to-replace-all-occurrence-of-a-character-except-last-in-a-string-in-ssis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try to use SSIS TOKEN() and TOKENCOUNT() functions.

TOKENCOUNT(column,".") == 4 ? TOKEN(1) + TOKEN(2) + TOKEN(3) + "." + TOKEN(4) : 
( TOKENCOUNT(column,".") == 3 ? TOKEN(1) + TOKEN(2) + "." + TOKEN(3) : 
( TOKENCOUNT(column,".") == 2 ? column : 
( TOKENCOUNT(column,".") == 0 ? column: “"  ) ) )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...