以下SQL Server 2008查询将名称列解析为单独的第一个,中间名和姓氏部分,并且工作得很好:
SELECT
LEFT(Name, CHARINDEX(' ', Name)) AS FirstName,
CASE WHEN CHARINDEX(' ', Name) <> LEN(Name) - CHARINDEX(' ', REVERSE(Name)) + 1 THEN
SUBSTRING(Name, CHARINDEX(' ', Name)+ 1, LEN(Name) - CHARINDEX(' ', REVERSE(Name))-CHARINDEX(' ', Name)) end as MiddleName,
RIGHT( Name, CHARINDEX(' ', REVERSE(Name))) AS LastName
FROM a01_parse_test
我现在需要将分隔的部分(FirstName
,MiddleName
和LastName
)存储到我的表格中(a01_parse_test
)。我怎么能这样做?
答案 0 :(得分:1)
好像您想要更新仅Name
FirstName
,MiddleName
,LastName
或TRIGGER
的不同更改的原始源表格。如果是这样,我会考虑在源表上创建一个视图,以标准化您的逻辑以拆分名称,然后在视图上使用INSTEAD OF UPDATE
添加IF OBJECT_ID('a01_parse_test') IS NULL BEGIN
CREATE TABLE a01_parse_test (Id INT PRIMARY KEY NOT NULL IDENTITY(1,1), Name VARCHAR(500))
INSERT a01_parse_test VALUES
('Barack Hussein Obama'),
('George Walker Bush'),
('William Jefferson Clinton'),
('Ronald Wilson Reagan')
END
IF OBJECT_ID('v_ParseNames') IS NOT NULL
DROP VIEW v_ParseNames
GO
CREATE VIEW v_ParseNames
AS
SELECT
Id, -- Whatever the id is of your parsing table
LEFT(Name, CHARINDEX(' ', Name)) AS FirstName,
CASE WHEN CHARINDEX(' ', Name) <> LEN(Name) - CHARINDEX(' ', REVERSE(Name)) + 1
THEN SUBSTRING(Name, CHARINDEX(' ', Name)+ 1, LEN(Name) - CHARINDEX(' ', REVERSE(Name))-CHARINDEX(' ', Name))
END AS MiddleName,
RIGHT(Name, CHARINDEX(' ', REVERSE(Name))) AS LastName
FROM a01_parse_test
GO
-- Create instead of update trigger on the view to update names separately
CREATE TRIGGER tg_v_ParseNames_Update ON v_ParseNames
INSTEAD OF UPDATE AS BEGIN
UPDATE TGT SET
Name = SRC.FirstName
+ (CASE WHEN LEN(SRC.MiddleName) > 0 THEN ' ' + SRC.MiddleName ELSE '' END)
+ ' ' + SRC.LastName
FROM a01_parse_test TGT
INNER JOIN INSERTED SRC
ON SRC.Id = TGT.Id
END
GO
SELECT * FROM v_ParseNames WHERE Id = 3
UPDATE v_ParseNames SET FirstName = 'Bill' WHERE Id = 3
SELECT * FROM v_ParseNames WHERE Id = 3
来处理您的更新。
以下完整示例:
Id FirstName MiddleName LastName
----------- --------------- --------------- ---------------
1 Barack Hussein Obama
2 George Walker Bush
3 William Jefferson Clinton
4 Ronald Wilson Reagan
所以在改变之前
UPDATE v_ParseNames SET FirstName = 'Bill' WHERE Id = 3
然后UPDATE语句可以只更新名字:
Id FirstName MiddleName LastName
----------- --------------- --------------- ---------------
3 Bill Jefferson Clinton
然后在更新语句运行之后:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="popupMenuStyle">@style/PopupMenu</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
答案 1 :(得分:0)
被修改
您可以直接在UPDATE
语句中使用这些函数(每this)。假设您的表格中有Name
,FirstName
,MiddleName
和LastName
列:
UPDATE a01_parse_test
SET FirstName=LEFT(Name, CHARINDEX(' ', Name)),
MiddleName=CASE WHEN CHARINDEX(' ', Name) <> LEN(Name) - CHARINDEX(' ', REVERSE(Name)) + 1 THEN
SUBSTRING(Name, CHARINDEX(' ', Name)+ 1, LEN(Name) - CHARINDEX(' ', REVERSE(Name))-CHARINDEX(' ', Name)) END,
LastName=RIGHT( Name, CHARINDEX(' ', REVERSE(Name)))
如果您的表格中不存在这些列,则您需要使用ALTER TABLE
添加这些列:
ALTER TABLE a01_parse_test
ADD FirstName VARCHAR(50) NULL,
MiddleName VARCHAR(50) NULL,
LastName VARCHAR(50) NULL