我想为下面的代码创建profileapplication的计算列。
Substring(Substring(P.propertyvaluesstring,
Charindex('ProfileApplication', P.propertyvaluesstring),
Charindex('</ProfileApplication',P.propertyvaluesstring) -
Charindex('ProfileApplication',P.propertyvaluesstring)),
Charindex('>', Substring(P.propertyvaluesstring,
Charindex( 'ProfileApplication',P.propertyvaluesstring),
Charindex('</ProfileApplication',P.propertyvaluesstring) - Charindex('ProfileApplication',P.propertyvaluesstring)))
+ 1, Len(Substring(P.propertyvaluesstring,
Charindex('ProfileApplication',
P.propertyvaluesstring),
Charindex('</ProfileApplication',
P.propertyvaluesstring) -
Charindex('ProfileApplication',
P.propertyvaluesstring)))) AS
ProfileApplication,
同样,我想使用计算列将ProfileApplication用于其他查询。我不确定但是有可能吗?
SUBSTRING
(SUBSTRING
(P.ProfileApplication,
CHARINDEX('RequisitionStartDate', P.ProfileApplication),
CHARINDEX('</RequisitionStartDate',P.ProfileApplication) -
CHARINDEX('RequisitionStartDate',P.ProfileApplication)
),
CHARINDEX('>', SUBSTRING(P.ProfileApplication,
CHARINDEX('RequisitionStartDate', P.ProfileApplication),
CHARINDEX('</RequisitionStartDate',P.ProfileApplication) -
CHARINDEX('RequisitionStartDate', P.ProfileApplication))) + 1,
LEN(SUBSTRING(P.ProfileApplication,
CHARINDEX('RequisitionStartDate',P.ProfileApplication),
CHARINDEX('</RequisitionStartDate',P.ProfileApplication) -
CHARINDEX('RequisitionStartDate',P.ProfileApplication))))
答案 0 :(得分:1)
假设propertyvaluesstring
列与计算列存在于同一个表中。
这是你需要做的:
ALTER TABLE <your table name here>
ADD ProfileApplication AS SUBSTRING(SUBSTRING(propertyvaluesstring, CHARINDEX('ProfileApplication', propertyvaluesstring), CHARINDEX('</ProfileApplication', propertyvaluesstring)-CHARINDEX('ProfileApplication', propertyvaluesstring)), CHARINDEX('>', SUBSTRING(propertyvaluesstring, CHARINDEX('ProfileApplication', propertyvaluesstring), CHARINDEX('</ProfileApplication', propertyvaluesstring)-CHARINDEX('ProfileApplication', propertyvaluesstring)))+1, LEN(SUBSTRING(propertyvaluesstring, CHARINDEX('ProfileApplication', propertyvaluesstring), CHARINDEX('</ProfileApplication', propertyvaluesstring)-CHARINDEX('ProfileApplication', propertyvaluesstring))))
请注意,计算列有时会对性能产生负面影响,因此在添加时我会保持谨慎。