0

I have a table, Salesperson, and the current name column shows the following:

Undefined - 0
Sam Brett-sbrett
Kelly Roberts-kroberts
Michael Paramore-mparamore
Alivia Lawler-alawler
Ryan Hooker-rhooker
Heather Alford-halford
Cassandra Blegen-cblegen
JD Holland-jholland
Vendor Accounts-VENDOR
Other Accounts-OTHER
Rose Johnson-rjohnson

I've developed the following query to extract the name (minus the ID on the end) with which I want to update this column:

SELECT left(salesperson_name, CHARINDEX('-', salesperson_name) - 1)
FROM Salesperson
------------------------
Undefined 
Sam Brett
Kelly Roberts
Michael Paramore
Alivia Lawler
Ryan Hooker
Heather Alford
Cassandra Blegen
JD Holland
Vendor Accounts
Other Accounts
Rose Johnson

I have attempted numerous variations of the following, but get the understandable

Subquery returned more than 1 value...' error.

begin tran
update salesperson
set salesperson_name = (SELECT left(salesperson_name, CHARINDEX('-', salesperson_name) - 1)
FROM Salesperson) 
1
  • The issue is that your subquery isn't correlated with the outer query, hence it processes the entire Salesperson table for each row in the table. Commented Jun 29, 2019 at 12:23

1 Answer 1

3

why not a simple

update salesperson
set  salesperson_name  = left(salesperson_name, CHARINDEX('-', salesperson_name) - 1)
Sign up to request clarification or add additional context in comments.

Comments

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.