从公式的除数中提取单引号之间的字符串部分

时间:2018-06-22 05:52:52

标签: sql sql-server tsql

select ATTRFormula,
       Devider_col=substring(attrformula,charindex('/',ATTRFormula)+9,len(attrformula)-2)
from temp_mst_measure 
where attrtype='derived' 
and attrformula like '%/%'

例如:

DMFunc('Food_01')/DMFunc('BTP_01'),这是Attrformula。

BTP_01'),这是Devider_col

如何在Devider_col中删除最后两个字符')

3 个答案:

答案 0 :(得分:0)

一种方法如下所示,获取除数部分,将单引号替换为点,然后使用parsename获得所需的内容。 阅读Parsename

另请参见 live demo

select ATTRFormula,
       parsename(
           replace(substring(ATTRFormula,charindex('/',ATTRFormula)+1, len(ATTRFormula)-charindex('/',ATTRFormula)),'''','.')
                   ,2)
from temp_mst_measure 
where attrtype='derived' 
and attrformula like '%/%'

答案 1 :(得分:0)

REPLACE用空字符串替换这些字符。

select ATTRFormula,
       Devider_col=
        REPLACE(
            substring(attrformula,charindex('/',ATTRFormula)+9,len(attrformula)-2),
            ''')',
            '')
from temp_mst_measure 
where attrtype='derived' 
and attrformula like '%/%'

答案 2 :(得分:0)

尝试一下:

select substring(AttrFormula, [start], [end] - [start]) from (
    select AttrFormula, [start], charindex('''', AttrFormula, [start] + 1) [end] from (
        select AttrFormula, charindex('''', AttrFormula, charindex('/', AttrFormula, 1)) + 1 [start]
        from MY_TABLE
    ) a
) a