0

I have a string BK1112-7. I want to replace everything after the -, including the -. How do I do that ? REPLACE() checks for a fixed for a fixed pattern. I want the pattern to be "variable".

How can I do this ?

0

2 Answers 2

2

You can do:

SELECT SUBSTRING('BK1112-7',0,CHARINDEX('-','BK1112-7'));

This will get you the value up until the -.

sqlfiddle demo

To add extra information after that just do + 'replacement'

Sign up to request clarification or add additional context in comments.

Comments

1
DECLARE @String NVARCHAR(20); 
SET @String  = 'BK1112-7';

SELECT LEFT(@String, CHARINDEX('-', @String) - 1) + 'ReplacementString'

Result

BK1112ReplacementString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.