Home > Enterprise >  Get every string before character in SQL Server
Get every string before character in SQL Server

Time:01-27

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?

CodePudding user response:

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
  • Related