2014-04-22 42 views
1
DECLARE @input NVARCHAR(MAX) = 'Create Procedure Test' 
DECLARE @SearhString NVARCHAR(MAX) = 'Procedure' 

DECLARE @index BIGINT = CHARINDEX(@keyword, @input) 

1 - 我想找到的下一個字符的長度字符串程序後啓動如何找到的第一個字符的SQL Server 2008開始

2 - 從那以後,我要檢查,如果DBO字符串在該長度之後存在於指定的位置。

假設第一個字符測試程序開始後第14位,所以我必須使用

DECLARE @String varchar(20) = SUBSTRING(@input, 21, 10) 

Declare @StringIndex BIGINT = CHARINDEX('dbo.', @String) 

Declare @FirstCharacterIndex BIGINT(This will be the index of the first character) 

如果@String不包含字符串DBO然後東西MYSCHEMA串測試前。

請建議。

UPDATE

我已經更新我的問題圖22是@index的結果+ @FirstCharacterIndex + 9

@index = 8

@FirstCharacterIndex = 4(3計數空間和第4個爲第一個字符)

9 =字符串的字符數程序

+0

你怎麼在這部分到達 「炮火下」 第一個字符指數( 「U」) - 'SUBSTRING(@input,13,10)'? –

+0

我已更新我的問題 – user3124690

回答

3

試試下面這個SQL:

DECLARE @input NVARCHAR(MAX) = 'Create Procedure Test'; 
DECLARE @input2 NVARCHAR(MAX) = 'Create Procedure dbo Test'; 
DECLARE @SearchString NVARCHAR(MAX) = 'Procedure'; 

DECLARE @index BIGINT = CHARINDEX(@SearchString, @input); 


select @input, @SearchString as searchstring, @index as Index_SearchString , @index +len(@SearchString) as Last_CharacterOfSearchString 
, charIndex('Test', @input,@index +len(@SearchString)) as test_Index 
, charIndex('dbo', @input,@index +len(@SearchString)) as dbo_Index 
, substring(@input, 0, @index +len(@SearchString)) as str1 
, substring(@input, @index +len(@SearchString), len(@input)[email protected] +len(@SearchString)) as str2 

Declare @dbo_Index int 
Declare @str1 nvarchar(max), @str2 nvarchar(max) 
set @dbo_Index = charIndex('dbo', @input,@index +len(@SearchString)) 

--select @dbo_Index 
if @dbo_Index = 0 
begin 
    set @str1 = substring(@input, 0, @index +len(@SearchString)) 
    set @str2 = substring(@input, @index +len(@SearchString), len(@input)[email protected] +len(@SearchString)) 

    select @str1 + ' ' + 'MySchema' + @str2 
end 

--,charIndex('dbo',@input2, @index +len(@SearchString)) as dbo_Index 

更新時間:
目標:獲取

DECLARE @input2 NVARCHAR(MAX) = 'Create Procedure Under Fire Test'; 
DECLARE @SearchString NVARCHAR(MAX) = 'Procedure'; 


--,charIndex('dbo',@input2, @index +len(@SearchString)) as dbo_Index 
select substring(@input2,17,25) 
, CHARINDEX(@SearchString,@input2,0) + len(@SearchString) as LastIndexOf_Procedure 
, CHARINDEX('Test',@input2,0) 
,len(@input2) 
,substring(@input2, CHARINDEX(@SearchString,@input2,0) + len(@SearchString), CHARINDEX('Test',@input2,0)- (CHARINDEX(@SearchString,@input2,0)+len(@SearchString))) 



set @SearchString= Ltrim(rtrim(substring(@input2, CHARINDEX(@SearchString,@input2,0) + len(@SearchString), CHARINDEX('Test',@input2,0)- (CHARINDEX(@SearchString,@input2,0)+len(@SearchString))))) 

select @SearchString 
select 
@input2 
,@SearchString as SearchString 
,CHARINDEX(@SearchString,@input2,0) as FirstCharacterIndex 
+0

我想查找過程後的第一個字符的索引不是「Test」,因爲字符串在飛行中變化 – user3124690

+0

我已經在選擇狀態 – cyan

+0

中包含test_index,然後才能找到過程之後的第一個字符的索引,需要在「Procedure」和「Test」之間使用子串字符串,在知道了字符串之後,可以使用charIndex來獲取第一個字符索引 – cyan

相關問題