如何在SQL Server 2012中修剪字符串

时间:2017-05-22 08:42:40

标签: sql sql-server

我有一个返回以下内容的select语句:

dbo.fn_Transactions_BatchTTotals/dbo.fn_ClientPortfolios_EFFECTS
dbo.fn_BTY_User/dbo.fn_Portfolios_Resolver/dbo.fn_Portfolios_NONE
dbo.fn_BTY_User/dbo.fn_Portfolios_Resolver/dbo.fn_Class_NONE
dbo.fn_Transactions_BatchTTotal/dbo.fn_Class_None

我只想留下以下内容:

fn_Transactions_BatchTTotals

fn_BTY_User  
fn_Transactions_BatchTTotal

我知道子字符串可以为我做的工作,但不知道如何做到这一点?我是sql的新手。换句话说,我想留下没有dbo部分的第一个部分。请帮帮忙?

1 个答案:

答案 0 :(得分:0)

declare @var varchar(max)

set @var='dbo.fn_Transactions_BatchTTotals/dbo.fn_ClientPortfolios_EFFECTS'

方法一:

select Substring(substring (@var,charindex('.',@var)+1,Len(@var)),1,
CHARINDEX('/',substring (@var,charindex('.',@var)+1,Len(@var)))-1)

从表中提取值(假设col1为colname,table1为table name:

select Substring(substring (col1 ,charindex('.',col1 )+1,Len(col1 )),1,
CHARINDEX('/',substring (col1 ,charindex('.',col1 )+1,Len(col1 )))-1)
from table1

方法2:

    select left(stuff(col1, 1, charindex('.', col1), ''),
        charindex('/', stuff(col1, 1, charindex('.', col1), '')) - 1
       ) from table1

方法3:

    select PARSENAME(Replace(col1,'/','.'),3) from table1