user3022742
user3022742

Reputation: 113

Remove sub-string from string

I have a column with entries in the format xxxx200_xx or in some instances xxx200_x.

Is it possible to remove the 200_ and the subsequent characters so that the remaining string is the 4, 5 or 6 digits before the 200_?

Thanks.

Upvotes: 0

Views: 45

Answers (1)

Francis P
Francis P

Reputation: 13655

Simply use LEFT and CHARINDEX like so:

SELECT LEFT(yourColumn, CHARINDEX('200_',yourColumn)-1);

So this:

SELECT LEFT('xxx200_xx', CHARINDEX('200_','xxx200_xx')-1);

Outputs this:

'xxx'

Upvotes: 2

Related Questions