0

I got two record in table which is as below -:

1.123-21
2.123-21-30

How to query for all string before certain place of character . Below shown expected output

1. 123-21 -> 123 
2. 123-21-30 ->123-21

How can I solve it?

7
  • 1
    CHARINDEX will find the - position. Then use SUBSTRING. Commented Jan 26, 2023 at 9:02
  • 1
    What is the logic here exactly? You don't actually explain it. You say you want the string before a certain characters, but what is that character. If it's -, which are both rows not 123? Commented Jan 26, 2023 at 9:02
  • Assuming something extremely simple: last 3 chars are always CharSpecialDigitDigit : Commented Jan 26, 2023 at 13:39
  • SELECT LEFT(Text, LEN() -2) Commented Jan 26, 2023 at 13:43
  • EzApi elixirforum.com/t/… Commented Jan 26, 2023 at 13:50

1 Answer 1

2
DECLARE @T TABLE (Vals VARCHAR(100))
INSERT INTO @T(Vals) VALUES ('123-21') , ('123-21-30')

SELECT LEFT(Vals, LEN(Vals) - CHARINDEX('-', REVERSE(Vals)) )
FROM @T
Sign up to request clarification or add additional context in comments.

2 Comments

thanks....that's what im looking for
NULLIF(CHARINDEX('-', REVERSE(Vals)), 0) would be wise, in case it can't find it, otherwise you will get errors.

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.