我需要在更新中使用下面的查询才能更新名字和姓氏。这样做的最佳选择是什么?
SELECT
person.fullName,
(CASE WHEN 0 = CHARINDEX(' ', person.fullName)
then person.fullName
ELSE SUBSTRING(person.fullName, 1, CHARINDEX(' ', person.fullName)) end) as first_name,
(CASE WHEN 0 = CHARINDEX(' ', person.fullName)
THEN ''
ELSE SUBSTRING(person.fullName,CHARINDEX(' ', person.fullName), LEN(person.fullName) )end) last_name
FROM person
谢谢。
答案 0 :(得分:1)
请尝试使用此代码,我没有修改您的条件,但向您展示了使用代码更新名字和姓氏的逻辑。我假设该表具有FirstName和LastName列
UPDATE Person
SET
Person.FirstName =
(CASE WHEN 0 = CHARINDEX(' ', person.fullName)
then person.fullName
ELSE SUBSTRING(person.fullName, 1, CHARINDEX(' ', person.fullName)) end) ,
Person.LastName =
(CASE WHEN 0 = CHARINDEX(' ', person.fullName)
THEN ''
ELSE SUBSTRING(person.fullName,CHARINDEX(' ', person.fullName), LEN(person.fullName) )end)
FROM person