2017-01-23 9 views
1

URLの途中でSQLの文字列を別のフォルダパスに置き換える必要があります。置換するフォルダのパスは、置換するフォルダのパスよりも小さく、大きく、同じサイズになります。 STUFFを使用しようとしていますが、挿入しているのと同じ数の文字が置き換えられます。STUFFを使用するSQLの文字列を、置換する文字列よりも小さい文字列で置換する

置き換える文字列の長さはどのように変更できますか? これは私がUDSを交換したいの下に+があなたの元の試みと

DECLARE @StartPosition int, @StringLength int 
DECLARE @NewFilePath varchar(max) = 'http://servername/ReportServer?%2fUDS+Reports%2fDSParameterizedDynamicReports%2fPatientsbyCode' 
DECLARE @ReportFilePath varchar(200) = 'UDSReports/PWReports' 
SELECT @ReportFilePath = REPLACE(REPLACE(@ReportFilePath, ' ', '+'), '/', '%2f') 
SELECT @ReportFilePath 
DECLARE @StartPosition int, @StringLength int, @ParameterList varchar(max) 
SELECT @StartPosition = CHARINDEX('%2f', @NewFilePath) + 1 
SELECT @StartPosition 
     SELECT @StringLength = REVERSE(CHARINDEX('%2f', @NewFilePath)) - @StartPosition 
SELECT @StringLength 
     SELECT @NewFilePath = STUFF(@NewFilePath, @StartPosition, @StringLength, @ReportFilePath) 
SELECT @NewFilePath 
+0

最終選択出力を書き出してください – EoinS

+0

@GloriaSantinこれについての最新情報はありますか? – SqlZim

答えて

0

一つの問題はREVERSE(CHARINDEX('%2f', @NewFilePath))CHARINDEX('f2%', reverse(@NewFilePath))

されているということです UDSReportsの%の2fPWReports持つ%の2fDSParameterizedDynamicReportsレポートの例では、私のSQL です

文字列の逆のパターンを探したら、パターンを逆にする必要がありますので、'%2f''f2%'になります。

もう1つの問題は、charindex()がパターンの開始位置を返すことです。したがって、3つの長さのパターンでは、それに応じて出力を調整する必要があります。

ここでものなしのものと1を使用して、2つの修正バージョンが、1以下のとおりです。

stuff()なし:stuff()で新しい中間

set @newfilepath = 
    left(@newfilepath,charindex('%2f',@newfilepath)+2) 
    [email protected] 
    +right(@newfilepath,charindex('f2%',reverse(@newfilepath))+2) 

周りの最初の文字列の先頭と末尾を連結するには:

rextester:http://rextester.com/GDFFV41538

declare @startposition int, @stringlength int, @parameterlist varchar(max) 

declare @newfilepath varchar(max) = 'http://servername/ReportServer?%2fUDS+Reports%2fDSParameterizedDynamicReports%2fPatientsbyCode' 
declare @reportfilepath varchar(200) = 'UDSReports/PWReports' 
select @reportfilepath = replace(replace(@reportfilepath, ' ', '+'), '/', '%2f') 

;with cte as (
select 
    LeftPart=left(@newfilepath,charindex('%2f',@newfilepath)+2) 
    , [email protected] 
    , RightPart=right(@newfilepath,charindex('f2%',reverse(@newfilepath))+2) 
    , woStuff= 
     left(@newfilepath,charindex('%2f',@newfilepath)+2) 
     [email protected] 
     +right(@newfilepath,charindex('f2%',reverse(@newfilepath))+2) 
    , wStuff = stuff(
     @newfilepath 
     , (charindex('%2f',@newfilepath)+3) 
     , len(@newfilepath) 
      -(charindex('%2f',@newfilepath)+2) 
      -(charindex('f2%',reverse(@newfilepath))+2) 
     ,@reportfilepath 
    ) 

) 
select 
    wostuff =replace(wostuff,'%2f','/') 
    , wstuff =replace(wstuff,'%2f','/') 
    , samesame=case when wStuff=woStuff then 'true' else 'false' end 
from cte 
関連する問題