2016-12-07 75 views
0

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

你可以包括你的表定义吗? –

+0

用你给我们的东西来回答这个问题是不可能的。一般来说,我们可以使用字符串函数在表**中创建计算列**。但是在你给我们的东西中,有一些像'P.propertyvaluesstring'这样的东西让我认为你想要引用其他表中的列,在我所知的范围内,除非你创建一个表,否则不应该这样做视图。但这是一个不同的概念。 – DVT

回答

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

注意计算列的某个时候产生不利影响性能,因此将它们时我会很谨慎。