如果内容中只有一个值,我想在最后删除逗号,但是应该存在一个以上的值 谢谢!
select
Names= (STUFF((
SELECT ', ' + CONCAT(
CASE WHEN RegistryUser.FirstName IS NOT NULL AND RegistryUser.LastName IS NOT NULL THEN RegistryUser.FirstName ELSE App.FirstName END,
' ',
CASE WHEN RegistryUser.FirstName IS NOT NULL AND RegistryUser.LastName IS NOT NULL THEN RegistryUser.LastName ELSE App.LastName END
) AS Names
FROM ConsultingEventConsultant
LEFT JOIN App
ON ConsultingEventConsultant.AppID = App.AppID
LEFT JOIN RegistryUser
ON App.UserID = RegistryUser.UserID
WHERE ConsultingEventConsultant.consultingeventid = 56428
and ConsultingEventConsultant.CECID != (SELECT TOP 1 CECID FROM ConsultingEventConsultant CEC WHERE CEC.ConsultingEventID = 56428 ORDER BY CECID)
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
), 1, 1, ''))
John Doe
约翰·多伊(Sara Smith)
答案 0 :(得分:1)
正如约翰·卡佩莱蒂(John Cappelleti)所述,您要删除前导逗号(尽管不是前导空格),并且如果有尾随逗号,则返回空白行。
要删除前导空格,只需将末尾的第二个1更改为2,如下所示。
select
Names= (STUFF((
SELECT ', ' + CONCAT(
CASE WHEN RegistryUser.FirstName IS NOT NULL AND RegistryUser.LastName IS NOT NULL THEN RegistryUser.FirstName ELSE App.FirstName END,
' ',
CASE WHEN RegistryUser.FirstName IS NOT NULL AND RegistryUser.LastName IS NOT NULL THEN RegistryUser.LastName ELSE App.LastName END
) AS Names
FROM ConsultingEventConsultant
LEFT JOIN App
ON ConsultingEventConsultant.AppID = App.AppID
LEFT JOIN RegistryUser
ON App.UserID = RegistryUser.UserID
WHERE ConsultingEventConsultant.consultingeventid = 56428
and ConsultingEventConsultant.CECID != (SELECT TOP 1 CECID FROM ConsultingEventConsultant CEC WHERE CEC.ConsultingEventID = 56428 ORDER BY CECID)
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
), 1, 2, ''))
要查看有关空白行的更多信息,请运行以下命令。然后,您应该能够找到错误的数据并修复它或查询中的错误逻辑(以引起空白行的情况为准)。
SELECT ', ' + CONCAT(
CASE WHEN RegistryUser.FirstName IS NOT NULL AND RegistryUser.LastName IS NOT NULL THEN RegistryUser.FirstName ELSE App.FirstName END,
' ',
CASE WHEN RegistryUser.FirstName IS NOT NULL AND RegistryUser.LastName IS NOT NULL THEN RegistryUser.LastName ELSE App.LastName END
) AS Names,
*
FROM ConsultingEventConsultant
LEFT JOIN App
ON ConsultingEventConsultant.AppID = App.AppID
LEFT JOIN RegistryUser
ON App.UserID = RegistryUser.UserID
WHERE ConsultingEventConsultant.consultingeventid = 56428
and ConsultingEventConsultant.CECID != (SELECT TOP 1 CECID FROM ConsultingEventConsultant CEC WHERE CEC.ConsultingEventID = 56428 ORDER BY CECID)
答案 1 :(得分:0)
我建议注意何时找不到名称。也许这满足您的需求:
void ThreadMain()
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
while( true )
{
// ...
Thread.Sleep( 250 );
}
process.CancelOutputRead();
process.CancelErrorRead();
Dispose();
}
或者,您可能想要添加如下逻辑:
select STUFF((SELECT ', ' +
COALESCE(ru.FirstName + ' ' + ru.LastName,
a.FirstName + ' ' + a.LastName,
'<NO NAME FOUND>'
) AS Names
FROM ConsultingEventConsultant c3c LEFT JOIN
App a
ON cec.AppID = a.AppID LEFT JOIN
RegistryUser ru
ON a.UserID = ru.UserID
WHERE cec.consultingeventid = 56428 AND
cec.CECID <> (SELECT TOP 1 cec2CECID
FROM ConsultingEventConsultant CEC2
WHERE CEC2.ConsultingEventID = 56428 ORDER BY CECID
)
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'
), 1, 2, ''
) as names
到 (ru.FirstName IS NOT NULL OR a.FirstName IS NOT NULL) AND
子句。