我在使用SQLServer2012时面临一个问题:我不能将包含unit word的功能替换为unit only。
所以这个代码返回
'FamilyUnit' as [FamilyUnit]
当我需要的时候
'FamilyUnit' as [Unit]
如果单词包含 Unit
,那么应该是 Unit
只是。
变化将在 AS
关键字。
create table #SplitNumberAndUnitsFinal
(
DKFeatureName nvarchar(100),
DisplayOrder int
)
insert into #SplitNumberAndUnitsFinal (DKFeatureName, DisplayOrder)
values ('package', 1), ('packageUnit', 1),
('Family', 2), ('FamilyUnit', 2),
('parts', 3), ('partsUnit', 3)
DECLARE @Header nvarchar(max) =
(SELECT SUBSTRING((SELECT ', ''' + DKFeatureName + ''' AS ['+ DKFeatureName +']' AS [text()]
FROM #SplitNumberAndUnitsFinal
GROUP BY DKFeatureName
ORDER BY MIN(DisplayOrder)
FOR XML PATH ('')), 2, 10000) [Columns])
PRINT @Header
预期结果是替换所有包含 Unit
只是 Unit
所以输出应该是:
'package' as [package],
'packageUnit' as [Unit],
'Family' as [Family],
'FamilyUnit' as [Unit],
'parts' as [parts],
'partsUnit' as [Unit]
1条答案
按热度按时间tyg4sfes1#
使用
CASE
表达式:引用标识符更安全
QUOTENAME
函数而不是添加[]
db<>小提琴演示