user3666224 user3666224 - 2 years ago 90
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 Source
SELECT 
  LEFT(LocationName, ISNULL(NULLIF(CHARINDEX('-',LocationName),0), LEN(LocationName)+1)-1)
 ,EncounterNumber
from #TubeRateQty
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download