简体   繁体   English

在字符串 [T-SQL] 中的第一个空格后插入逗号

[英]Insert a comma after first space in string [T-SQL]

I have a field that contains full names like "Last First Middle" with no comma.我有一个字段,其中包含不带逗号的“Last First Middle”等全名。

I want to put a comma after the last name like "Last, First Middle".我想在姓氏后面加一个逗号,比如“Last, First Middle”。

Use the CHARINDEX function to find the first space in the string and then use the STUFF function to stuff a comma in there.使用 CHARINDEX 函数查找字符串中的第一个空格,然后使用 STUFF 函数在其中填充逗号。

DECLARE @FullName as NVARCHAR(50)

SET @FullName = 'Harris Neil Patrick'

SELECT STUFF(@FullName, CHARINDEX(' ', @FullName, 0), 0, ',');

returns 'Harris, Neil Patrick'返回“哈里斯,尼尔帕特里克”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM