SELECT CustomerName
,LEFT(CustomerName, CHARINDEX(',', CustomerName) - 1) AS [Surname]
,REPLACE(SUBSTRING(CustomerName, CHARINDEX(',', CustomerName), LEN(CustomerName)), '(', '') AS CitySt
FROM Sales.Customers CUST
declare @a varchar(100) = 'TKSuppliers (ABCity, TX)'
SELECT @a, charindex(' (', @a), charindex(', ',@a)
; with cte as
(select @a a, len(@a) len, charindex(' (', @a) first , charindex(', ',@a) second
)
select a, substring(a, 1, first) Customer, substring(a, first+2, second-first-2) City, ltrim(replace(right(a, len-second), ')', '')) state
from cte
; with cte as (
SELECT CustomerName
, len(CustomerName) len, charindex(' (', CustomerName) first , charindex(', ',CustomerName) second
FROM Sales.Customers
)
SELECT CustomerName
, substring(CustomerName, 1, first-1) Customer, substring(CustomerName, first+2, second-first-2) City, ltrim(replace(right(CustomerName, len-second), ')', '')) state
FROM cte