基于函数的计算列

时间:2016-12-07 17:59:06

标签: tsql

我想为下面的代码创建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)))) 

1 个答案:

答案 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)))) 

请注意,计算列有时会对性能产生负面影响,因此在添加时我会保持谨慎。