user3666224 user3666224 - 5 months ago 14
SQL Question

Extract text to the left of the '-'

I am trying to get the data to the left of the - I tried the code below

LocationName
CCC-CC36-A
5N-N577-A
5N-N577-A
5N-N577-A
2T-225-A
PE-020-A

select
LEFT(LocationName, LEN(LocationName) - CHARINDEX('-',LocationName)-1),
EncounterNumber
from #TubeRateQty

Answer
SELECT 
  LEFT(LocationName, ISNULL(NULLIF(CHARINDEX('-',LocationName),0), LEN(LocationName)+1)-1)
 ,EncounterNumber
from #TubeRateQty