2017-06-02 63 views
1

我有字段名以下字符串的SQL字符串換行:插入基於特定的時間間隔/字符

'TransID, Convert(VarChar(250), [TPSYN]) AS [TPSYN], Convert(VarChar(250), [SureFitYN]) AS [SureFitYN], Convert(VarChar(250), [AccountNbr]) AS [AccountNbr], Convert(VarChar(250), [Account Name]) AS [Account Name], Convert(VarChar(250), [EffectiveDate]) AS [EffectiveDate], Convert(VarChar(250), [IM]) AS [IM], Convert(VarChar(250), [RevisionDate]) AS [RevisionDate], Convert(VarChar(250), [AccountAddress]) AS [AccountAddress], Convert(VarChar(250), [LSOCode]) AS [LSOCode], Convert(VarChar(250), [ASONonASOBoth]) AS [ASONonASOBoth], Convert(VarChar(250), [MedicalYN]) AS [MedicalYN], Convert(VarChar(250), [NonManagedCareYN]) AS [NonManagedCareYN], Convert(VarChar(250), [ManagedCareYN]) AS [ManagedCareYN], Convert(VarChar(250), [HRAYN]) AS [HRAYN], Convert(VarChar(250), [MedHRAYN]) AS [MedHRAYN], Convert(VarChar(250), [LPHRAYN]) AS [LPHRAYN], Convert(VarChar(250), [HealthyFutYN]) AS [HealthyFutYN], Convert(VarChar(250), [HAHRAYN]) AS [HAHRAYN], Convert(VarChar(250), [HSAYN]) AS [HSAYN], Convert(VarChar(25 
0), [FSAHealthCare]) AS [FSAHealthCare], Convert(VarChar(250), [FSADependentCare]) AS [FSADependentCare], Convert(VarChar(250), [DentalYN]) AS [DentalYN], Convert(VarChar(250), [TaftHartley]) AS [TaftHartley], Convert(VarChar(250), [ParameterComments]) AS [ParameterComments], Convert(VarChar(250), [MLRAvgLivesNumber]) AS [MLRAvgLivesNumber], Convert(VarChar(250), [MLRAvgLivesRptYear]) AS [MLRAvgLivesRptYear], Convert(VarChar(250), [MLRContact]) AS [MLRContact], Convert(VarChar(250), [MLRContactAddress1]) AS [MLRContactAddress1], Convert(VarChar(250), [MLRContactAddress2]) AS [MLRContactAddress2], Convert(VarChar(250), [MLRContactCity]) AS [MLRContactCity], Convert(VarChar(250), [MLRContactState]) AS [MLRContactState], Convert(VarChar(250), [MLRContactZip]) AS [MLRContactZip], Convert(VarChar(250), [HealthRiskAssessment]) AS [HealthRiskAssessment], Convert(VarChar(250), [ErisaYN]) AS [ErisaYN], Convert(VarChar(250), [ErisaPlanName]) AS [ErisaPlanName], Convert(VarChar(250), [ErisaPlanNumber]) AS [ErisaPlanNum 
ber], Convert(VarChar(250), [ErisaPlanEndsMM]) AS [ErisaPlanEndsMM], Convert(VarChar(250), [ErisaPlanEndsDD]) AS [ErisaPlanEndsDD], Convert(VarChar(250), [ErisaAdministrator]) AS [ErisaAdministrator], Convert(VarChar(250), [ErisaClaimAdministrator]) AS [ErisaClaimAdministrator], Convert(VarChar(250), [ErisaCost]) AS [ErisaCost], Convert(VarChar(250), [ErisaEOB]) AS [ErisaEOB], Convert(VarChar(250), [ErisaEOBName]) AS [ErisaEOBName], Convert(VarChar(250), [ErisaEOBAddress1]) AS [ErisaEOBAddress1], Convert(VarChar(250), [ErisaEOBAddress2]) AS [ErisaEOBAddress2], Convert(VarChar(250), [ErisaEOBCity]) AS [ErisaEOBCity], Convert(VarChar(250), [ErisaEOBState]) AS [ErisaEOBState], Convert(VarChar(250), [ErisaEOBZip]) AS [ErisaEOBZip], Convert(VarChar(250), [ErisaEOBPhone]) AS [ErisaEOBPhone], Convert(VarChar(250), [ErisaPAName]) AS [ErisaPAName], Convert(VarChar(250), [ErisaPAAddress1]) AS [ErisaPAAddress1], Convert(VarChar(250), [ErisaPAAddress2]) AS [ErisaPAAddress2], Convert(VarChar(250), [ErisaPACity]) AS [Eris 
aPACity], Convert(VarChar(250), [ErisaPAState]) AS [ErisaPAState], Convert(VarChar(250), [ErisaPAZip]) AS [ErisaPAZip], Convert(VarChar(250), [ErisaPAPhone]) AS [ErisaPAPhone], Convert(VarChar(250), [ErisaLAName]) AS [ErisaLAName], Convert(VarChar(250), [ErisaLAAddress1]) AS [ErisaLAAddress1], Convert(VarChar(250), [ErisaLAAddress2]) AS [ErisaLAAddress2], Convert(VarChar(250), [ErisaLACity]) AS [ErisaLACity], Convert(VarChar(250), [ErisaLAState]) AS [ErisaLAState], Convert(VarChar(250), [ErisaLAZip]) AS [ErisaLAZip], Convert(VarChar(250), [ErisaLAPhone]) AS [ErisaLAPhone], Convert(VarChar(250), [ERISAComments]) AS [ERISAComments], Convert(VarChar(250), [CSNApproval]) AS [CSNApproval], Convert(VarChar(250), [CSNPPO]) AS [CSNPPO], Convert(VarChar(250), [CSNOAP]) AS [CSNOAP], Convert(VarChar(250), [CSNSupplemental]) AS [CSNSupplemental], Convert(VarChar(250), [CSNReplacement]) AS [CSNReplacement], Convert(VarChar(250), [CSNType]) AS [CSNType], Convert(VarChar(250), [CSNNBNIL915]) AS [CSNNBNIL915], Convert(VarCh 
ar(250), [CSNNBNPA910]) AS [CSNNBNPA910], Convert(VarChar(250), [CSNNBNPA911]) AS [CSNNBNPA911], Convert(VarChar(250), [CSNNBNFL918]) AS [CSNNBNFL918], Convert(VarChar(250), [CSNNBNCA915]) AS [CSNNBNCA915], Convert(VarChar(250), [CSNNBNMA904]) AS [CSNNBNMA904], Convert(VarChar(250), [CSNOwnLogo]) AS [CSNOwnLogo], Convert(VarChar(250), [CSNNBNAllBranches]) AS [CSNNBNAllBranches], Convert(VarChar(250), [CSNNBNBranchComment]) AS [CSNNBNBranchComment], Convert(VarChar(250), [ASOStopLoss]) AS [ASOStopLoss], Convert(VarChar(250), [ASOCigna]) AS [ASOCigna], Convert(VarChar(250), [ASOThirdParty]) AS [ASOThirdParty], Convert(VarChar(250), [ASORetirees]) AS [ASORetirees], Convert(VarChar(250), [ISOStopLoss]) AS [ISOStopLoss], Convert(VarChar(250), [ISOCigna]) AS [ISOCigna], Convert(VarChar(250), [ISOThirdParty]) AS [ISOThirdParty], Convert(VarChar(250), [ISORetirees]) AS [ISORetirees], Convert(VarChar(250), [ASOReimbCigna]) AS [ASOReimbCigna], Convert(VarChar(250), [ASOReimbOther]) AS [ASOReimbOther]' 

我需要做的是插入換行符所以沒有線長度超過950個字符,但我需要在字段名稱末尾插入這些中斷。所以,我需要做的是從字符950開始並向後工作,直到找到「],」的第一個實例。我需要在那裏插入換行符,然後向前計算950個字符,並再次向後工作,直到我點擊「],」的第一個實例。泡沫,沖洗,重複。

結果字符串應該結束了尋找這樣的事情(除非我算錯了):

TransID, Convert(VarChar(250), [TPSYN]) AS [TPSYN], Convert(VarChar(250), [SureFitYN]) AS [SureFitYN], Convert(VarChar(250), [AccountNbr]) AS [AccountNbr], Convert(VarChar(250), [Account Name]) AS [Account Name], Convert(VarChar(250), [EffectiveDate]) AS [EffectiveDate], Convert(VarChar(250), [IM]) AS [IM], Convert(VarChar(250), [RevisionDate]) AS [RevisionDate], Convert(VarChar(250), [AccountAddress]) AS [AccountAddress], Convert(VarChar(250), [LSOCode]) AS [LSOCode], Convert(VarChar(250), [ASONonASOBoth]) AS [ASONonASOBoth], Convert(VarChar(250), [MedicalYN]) AS [MedicalYN], Convert(VarChar(250), [NonManagedCareYN]) AS [NonManagedCareYN], Convert(VarChar(250), [ManagedCareYN]) AS [ManagedCareYN], Convert(VarChar(250), [HRAYN]) AS [HRAYN], Convert(VarChar(250), [MedHRAYN]) AS [MedHRAYN], Convert(VarChar(250), [LPHRAYN]) AS [LPHRAYN], Convert(VarChar(250), [HealthyFutYN]) AS [HealthyFutYN], Convert(VarChar(250), [HAHRAYN]) AS [HAHRAYN], 
Convert(VarChar(250), [HSAYN]) AS [HSAYN], Convert(VarChar(250), [FSAHealthCare]) AS [FSAHealthCare], Convert(VarChar(250), [FSADependentCare]) AS [FSADependentCare], Convert(VarChar(250), [DentalYN]) AS [DentalYN], Convert(VarChar(250), [TaftHartley]) AS [TaftHartley], Convert(VarChar(250), [ParameterComments]) AS [ParameterComments], Convert(VarChar(250), [MLRAvgLivesNumber]) AS [MLRAvgLivesNumber], Convert(VarChar(250), [MLRAvgLivesRptYear]) AS [MLRAvgLivesRptYear], Convert(VarChar(250), [MLRContact]) AS [MLRContact], Convert(VarChar(250), [MLRContactAddress1]) AS [MLRContactAddress1], Convert(VarChar(250), [MLRContactAddress2]) AS [MLRContactAddress2], Convert(VarChar(250), [MLRContactCity]) AS [MLRContactCity], Convert(VarChar(250), [MLRContactState]) AS [MLRContactState], Convert(VarChar(250), [MLRContactZip]) AS [MLRContactZip], Convert(VarChar(250), [HealthRiskAssessment]) AS [HealthRiskAssessment], Convert(VarChar(250), [ErisaYN]) AS [ErisaYN], 
Convert(VarChar(250), [ErisaPlanName]) AS [ErisaPlanName], Convert(VarChar(250), [ErisaPlanNumber]) AS [ErisaPlanNumber], Convert(VarChar(250), [ErisaPlanEndsMM]) AS [ErisaPlanEndsMM], Convert(VarChar(250), [ErisaPlanEndsDD]) AS [ErisaPlanEndsDD], Convert(VarChar(250), [ErisaAdministrator]) AS [ErisaAdministrator], Convert(VarChar(250), [ErisaClaimAdministrator]) AS [ErisaClaimAdministrator], Convert(VarChar(250), [ErisaCost]) AS [ErisaCost], Convert(VarChar(250), [ErisaEOB]) AS [ErisaEOB], Convert(VarChar(250), [ErisaEOBName]) AS [ErisaEOBName], Convert(VarChar(250), [ErisaEOBAddress1]) AS [ErisaEOBAddress1], Convert(VarChar(250), [ErisaEOBAddress2]) AS [ErisaEOBAddress2], Convert(VarChar(250), [ErisaEOBCity]) AS [ErisaEOBCity], Convert(VarChar(250), [ErisaEOBState]) AS [ErisaEOBState], Convert(VarChar(250), [ErisaEOBZip]) AS [ErisaEOBZip], Convert(VarChar(250), [ErisaEOBPhone]) AS [ErisaEOBPhone], Convert(VarChar(250), [ErisaPAName]) AS [ErisaPAName], 
Convert(VarChar(250), [ErisaPAAddress1]) AS [ErisaPAAddress1], Convert(VarChar(250), [ErisaPAAddress2]) AS [ErisaPAAddress2], Convert(VarChar(250), [ErisaPACity]) AS [ErisaPACity], Convert(VarChar(250), [ErisaPAState]) AS [ErisaPAState], Convert(VarChar(250), [ErisaPAZip]) AS [ErisaPAZip], Convert(VarChar(250), [ErisaPAPhone]) AS [ErisaPAPhone], Convert(VarChar(250), [ErisaLAName]) AS [ErisaLAName], Convert(VarChar(250), [ErisaLAAddress1]) AS [ErisaLAAddress1], Convert(VarChar(250), [ErisaLAAddress2]) AS [ErisaLAAddress2], Convert(VarChar(250), [ErisaLACity]) AS [ErisaLACity], Convert(VarChar(250), [ErisaLAState]) AS [ErisaLAState], Convert(VarChar(250), [ErisaLAZip]) AS [ErisaLAZip], Convert(VarChar(250), [ErisaLAPhone]) AS [ErisaLAPhone], Convert(VarChar(250), [ERISAComments]) AS [ERISAComments], Convert(VarChar(250), [CSNApproval]) AS [CSNApproval], Convert(VarChar(250), [CSNPPO]) AS [CSNPPO], Convert(VarChar(250), [CSNOAP]) AS [CSNOAP], 
Convert(VarChar(250), [CSNSupplemental]) AS [CSNSupplemental], Convert(VarChar(250), [CSNReplacement]) AS [CSNReplacement], Convert(VarChar(250), [CSNType]) AS [CSNType], Convert(VarChar(250), [CSNNBNIL915]) AS [CSNNBNIL915], Convert(VarChar(250), [CSNNBNPA910]) AS [CSNNBNPA910], Convert(VarChar(250), [CSNNBNPA911]) AS [CSNNBNPA911], Convert(VarChar(250), [CSNNBNFL918]) AS [CSNNBNFL918], Convert(VarChar(250), [CSNNBNCA915]) AS [CSNNBNCA915], Convert(VarChar(250), [CSNNBNMA904]) AS [CSNNBNMA904], Convert(VarChar(250), [CSNOwnLogo]) AS [CSNOwnLogo], Convert(VarChar(250), [CSNNBNAllBranches]) AS [CSNNBNAllBranches], Convert(VarChar(250), [CSNNBNBranchComment]) AS [CSNNBNBranchComment], Convert(VarChar(250), [ASOStopLoss]) AS [ASOStopLoss], Convert(VarChar(250), [ASOCigna]) AS [ASOCigna], Convert(VarChar(250), [ASOThirdParty]) AS [ASOThirdParty], Convert(VarChar(250), [ASORetirees]) AS [ASORetirees], Convert(VarChar(250), [ISOStopLoss]) AS [ISOStopLoss], 
Convert(VarChar(250), [ISOCigna]) AS [ISOCigna], Convert(VarChar(250), [ISOThirdParty]) AS [ISOThirdParty], Convert(VarChar(250), [ISORetirees]) AS [ISORetirees], Convert(VarChar(250), [ASOReimbCigna]) AS [ASOReimbCigna], Convert(VarChar(250), [ASOReimbOther]) AS [ASOReimbOther] 

字符串幾乎總是不斷變化的,所以我不能硬編碼的中斷,它一定是動態完成。

我知道一點SQL但不足以做到這一點,我甚至不知道從哪裏開始。

有人給了我這個代碼,但它的作用是第950個字符之後插入一個換行符:

declare @var nvarchar(max) = 'SELECT TransID, Convert(VarChar(250), [TPSYN]) AS [TPSYN], Convert(VarChar(250), [SureFitYN]) AS [SureFitYN], Convert(VarChar(250), [AccountNbr]) AS [AccountNbr], Convert(VarChar(250), [Account Name]) AS [Account Name], Convert(VarChar(250), [EffectiveDate]) AS [EffectiveDate], Convert(VarChar(250), [IM]) AS [IM], Convert(VarChar(250), [RevisionDate]) AS [RevisionDate], Convert(VarChar(250), [AccountAddress]) AS [AccountAddress], Convert(VarChar(250), [LSOCode]) AS [LSOCode], Convert(VarChar(250), [ASONonASOBoth]) AS [ASONonASOBoth], Convert(VarChar(250), [MedicalYN]) AS [MedicalYN], Convert(VarChar(250), [NonManagedCareYN]) AS [NonManagedCareYN], Convert(VarChar(250), [ManagedCareYN]) AS [ManagedCareYN], Convert(VarChar(250), [HRAYN]) AS [HRAYN], Convert(VarChar(250), [MedHRAYN]) AS [MedHRAYN], Convert(VarChar(250), [LPHRAYN]) AS [LPHRAYN], Convert(VarChar(250), [HealthyFutYN]) AS [HealthyFutYN], Convert(VarChar(250), [HAHRAYN]) AS [HAHRAYN], Convert(VarChar(250), [HSAYN]) AS [HSAYN], Convert(VarChar(25 
0), [FSAHealthCare]) AS [FSAHealthCare], Convert(VarChar(250), [FSADependentCare]) AS [FSADependentCare], Convert(VarChar(250), [DentalYN]) AS [DentalYN], Convert(VarChar(250), [TaftHartley]) AS [TaftHartley], Convert(VarChar(250), [ParameterComments]) AS [ParameterComments], Convert(VarChar(250), [MLRAvgLivesNumber]) AS [MLRAvgLivesNumber], Convert(VarChar(250), [MLRAvgLivesRptYear]) AS [MLRAvgLivesRptYear], Convert(VarChar(250), [MLRContact]) AS [MLRContact], Convert(VarChar(250), [MLRContactAddress1]) AS [MLRContactAddress1], Convert(VarChar(250), [MLRContactAddress2]) AS [MLRContactAddress2], Convert(VarChar(250), [MLRContactCity]) AS [MLRContactCity], Convert(VarChar(250), [MLRContactState]) AS [MLRContactState], Convert(VarChar(250), [MLRContactZip]) AS [MLRContactZip], Convert(VarChar(250), [HealthRiskAssessment]) AS [HealthRiskAssessment], Convert(VarChar(250), [ErisaYN]) AS [ErisaYN], Convert(VarChar(250), [ErisaPlanName]) AS [ErisaPlanName], Convert(VarChar(250), [ErisaPlanNumber]) AS [ErisaPlanNum 
ber], Convert(VarChar(250), [ErisaPlanEndsMM]) AS [ErisaPlanEndsMM], Convert(VarChar(250), [ErisaPlanEndsDD]) AS [ErisaPlanEndsDD], Convert(VarChar(250), [ErisaAdministrator]) AS [ErisaAdministrator], Convert(VarChar(250), [ErisaClaimAdministrator]) AS [ErisaClaimAdministrator], Convert(VarChar(250), [ErisaCost]) AS [ErisaCost], Convert(VarChar(250), [ErisaEOB]) AS [ErisaEOB], Convert(VarChar(250), [ErisaEOBName]) AS [ErisaEOBName], Convert(VarChar(250), [ErisaEOBAddress1]) AS [ErisaEOBAddress1], Convert(VarChar(250), [ErisaEOBAddress2]) AS [ErisaEOBAddress2], Convert(VarChar(250), [ErisaEOBCity]) AS [ErisaEOBCity], Convert(VarChar(250), [ErisaEOBState]) AS [ErisaEOBState], Convert(VarChar(250), [ErisaEOBZip]) AS [ErisaEOBZip], Convert(VarChar(250), [ErisaEOBPhone]) AS [ErisaEOBPhone], Convert(VarChar(250), [ErisaPAName]) AS [ErisaPAName], Convert(VarChar(250), [ErisaPAAddress1]) AS [ErisaPAAddress1], Convert(VarChar(250), [ErisaPAAddress2]) AS [ErisaPAAddress2], Convert(VarChar(250), [ErisaPACity]) AS [Eris 
aPACity], Convert(VarChar(250), [ErisaPAState]) AS [ErisaPAState], Convert(VarChar(250), [ErisaPAZip]) AS [ErisaPAZip], Convert(VarChar(250), [ErisaPAPhone]) AS [ErisaPAPhone], Convert(VarChar(250), [ErisaLAName]) AS [ErisaLAName], Convert(VarChar(250), [ErisaLAAddress1]) AS [ErisaLAAddress1], Convert(VarChar(250), [ErisaLAAddress2]) AS [ErisaLAAddress2], Convert(VarChar(250), [ErisaLACity]) AS [ErisaLACity], Convert(VarChar(250), [ErisaLAState]) AS [ErisaLAState], Convert(VarChar(250), [ErisaLAZip]) AS [ErisaLAZip], Convert(VarChar(250), [ErisaLAPhone]) AS [ErisaLAPhone], Convert(VarChar(250), [ERISAComments]) AS [ERISAComments], Convert(VarChar(250), [CSNApproval]) AS [CSNApproval], Convert(VarChar(250), [CSNPPO]) AS [CSNPPO], Convert(VarChar(250), [CSNOAP]) AS [CSNOAP], Convert(VarChar(250), [CSNSupplemental]) AS [CSNSupplemental], Convert(VarChar(250), [CSNReplacement]) AS [CSNReplacement], Convert(VarChar(250), [CSNType]) AS [CSNType], Convert(VarChar(250), [CSNNBNIL915]) AS [CSNNBNIL915], Convert(VarCh 
ar(250), [CSNNBNPA910]) AS [CSNNBNPA910], Convert(VarChar(250), [CSNNBNPA911]) AS [CSNNBNPA911], Convert(VarChar(250), [CSNNBNFL918]) AS [CSNNBNFL918], Convert(VarChar(250), [CSNNBNCA915]) AS [CSNNBNCA915], Convert(VarChar(250), [CSNNBNMA904]) AS [CSNNBNMA904], Convert(VarChar(250), [CSNOwnLogo]) AS [CSNOwnLogo], Convert(VarChar(250), [CSNNBNAllBranches]) AS [CSNNBNAllBranches], Convert(VarChar(250), [CSNNBNBranchComment]) AS [CSNNBNBranchComment], Convert(VarChar(250), [ASOStopLoss]) AS [ASOStopLoss], Convert(VarChar(250), [ASOCigna]) AS [ASOCigna], Convert(VarChar(250), [ASOThirdParty]) AS [ASOThirdParty], Convert(VarChar(250), [ASORetirees]) AS [ASORetirees], Convert(VarChar(250), [ISOStopLoss]) AS [ISOStopLoss], Convert(VarChar(250), [ISOCigna]) AS [ISOCigna], Convert(VarChar(250), [ISOThirdParty]) AS [ISOThirdParty], Convert(VarChar(250), [ISORetirees]) AS [ISORetirees], Convert(VarChar(250), [ASOReimbCigna]) AS [ASOReimbCigna], Convert(VarChar(250), [ASOReimbOther]) AS [ASOReimbOther]' 



Declare @a table (pos int, rn int identity(1,1)) 
Declare @ct int = 0 
Declare @pos int 
Declare @oldpos int 
Select @oldpos=0 
select @pos=charindex(char(10),@var) 
while @ct <= (select len(@var) - len(replace(@var,char(10),''))) 
    begin 
    insert into @a Values (@pos) 
    Select @[email protected] 
    select @pos=charindex(char(10),Substring(@var,@pos + 1,len(@var))) + @pos 
    select @ct = @ct + 1 
end 

Select 
    --*, 
    substring(@var,isnull(a2.pos,0),a1.pos) as SubSet 
from @a a1 
left join @a a2 on a2.rn = a1.rn - 1 


--edit for your comment... 

select 
    --forces a CR at the comma before 950 characters 
    stuff(replace(@var,char(10),''),charindex('],',replace(@var,char(10),''),950),0,CHAR(13)+CHAR(10)) 
+1

[打破串起來,當它充滿了表的字段名]的可能的複製(https://開頭計算器.com/questions/44314803 /使用表格字段名稱填充字符時) – scsimon

+0

是的,我想刪除那一個。 –

+0

這是重複的,你從一開始就不清楚。在不滿意之後,您不斷更改問題和要求。你的配置文件說你是一個SQL服務器主,但問題不是[MCVE](https://stackoverflow.com/help/mcve),你需要[真正學習如何提出sql問題](https:/ /spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/) – scsimon

回答

3
Declare @S varchar(max) = 'TransID, Convert(VarChar(250), [TPSYN]) AS [TPSYN], Convert(VarChar(250), [SureFitYN]) AS [SureFitYN], Convert(VarChar(250), [AccountNbr]) AS [AccountNbr], Convert(VarChar(250), [Account Name]) AS [Account Name], Convert(VarChar(250), [EffectiveDate]) AS [EffectiveDate], Convert(VarChar(250), [IM]) AS [IM], Convert(VarChar(250), [RevisionDate]) AS [RevisionDate], Convert(VarChar(250), [AccountAddress]) AS [AccountAddress], Convert(VarChar(250), [LSOCode]) AS [LSOCode], Convert(VarChar(250), [ASONonASOBoth]) AS [ASONonASOBoth], Convert(VarChar(250), [MedicalYN]) AS [MedicalYN], Convert(VarChar(250), [NonManagedCareYN]) AS [NonManagedCareYN], Convert(VarChar(250), [ManagedCareYN]) AS [ManagedCareYN], Convert(VarChar(250), [HRAYN]) AS [HRAYN], Convert(VarChar(250), [MedHRAYN]) AS [MedHRAYN], Convert(VarChar(250), [LPHRAYN]) AS [LPHRAYN], Convert(VarChar(250), [HealthyFutYN]) AS [HealthyFutYN], Convert(VarChar(250), [HAHRAYN]) AS [HAHRAYN], Convert(VarChar(250), [HSAYN]) AS [HSAYN], Convert(VarChar(250), [FSAHealthCare]) AS [FSAHealthCare], Convert(VarChar(250), [FSADependentCare]) AS [FSADependentCare], Convert(VarChar(250), [DentalYN]) AS [DentalYN], Convert(VarChar(250), [TaftHartley]) AS [TaftHartley], Convert(VarChar(250), [ParameterComments]) AS [ParameterComments], Convert(VarChar(250), [MLRAvgLivesNumber]) AS [MLRAvgLivesNumber], Convert(VarChar(250), [MLRAvgLivesRptYear]) AS [MLRAvgLivesRptYear], Convert(VarChar(250), [MLRContact]) AS [MLRContact], Convert(VarChar(250), [MLRContactAddress1]) AS [MLRContactAddress1], Convert(VarChar(250), [MLRContactAddress2]) AS [MLRContactAddress2], Convert(VarChar(250), [MLRContactCity]) AS [MLRContactCity], Convert(VarChar(250), [MLRContactState]) AS [MLRContactState], Convert(VarChar(250), [MLRContactZip]) AS [MLRContactZip], Convert(VarChar(250), [HealthRiskAssessment]) AS [HealthRiskAssessment], Convert(VarChar(250), [ErisaYN]) AS [ErisaYN], Convert(VarChar(250), [ErisaPlanName]) AS [ErisaPlanName], Convert(VarChar(250), [ErisaPlanNumber]) AS [ErisaPlanNumber], Convert(VarChar(250), [ErisaPlanEndsMM]) AS [ErisaPlanEndsMM], Convert(VarChar(250), [ErisaPlanEndsDD]) AS [ErisaPlanEndsDD], Convert(VarChar(250), [ErisaAdministrator]) AS [ErisaAdministrator], Convert(VarChar(250), [ErisaClaimAdministrator]) AS [ErisaClaimAdministrator], Convert(VarChar(250), [ErisaCost]) AS [ErisaCost], Convert(VarChar(250), [ErisaEOB]) AS [ErisaEOB], Convert(VarChar(250), [ErisaEOBName]) AS [ErisaEOBName], Convert(VarChar(250), [ErisaEOBAddress1]) AS [ErisaEOBAddress1], Convert(VarChar(250), [ErisaEOBAddress2]) AS [ErisaEOBAddress2], Convert(VarChar(250), [ErisaEOBCity]) AS [ErisaEOBCity], Convert(VarChar(250), [ErisaEOBState]) AS [ErisaEOBState], Convert(VarChar(250), [ErisaEOBZip]) AS [ErisaEOBZip], Convert(VarChar(250), [ErisaEOBPhone]) AS [ErisaEOBPhone], Convert(VarChar(250), [ErisaPAName]) AS [ErisaPAName], Convert(VarChar(250), [ErisaPAAddress1]) AS [ErisaPAAddress1], Convert(VarChar(250), [ErisaPAAddress2]) AS [ErisaPAAddress2], Convert(VarChar(250), [ErisaPACity]) AS [ErisaPACity], Convert(VarChar(250), [ErisaPAState]) AS [ErisaPAState], Convert(VarChar(250), [ErisaPAZip]) AS [ErisaPAZip], Convert(VarChar(250), [ErisaPAPhone]) AS [ErisaPAPhone], Convert(VarChar(250), [ErisaLAName]) AS [ErisaLAName], Convert(VarChar(250), [ErisaLAAddress1]) AS [ErisaLAAddress1], Convert(VarChar(250), [ErisaLAAddress2]) AS [ErisaLAAddress2], Convert(VarChar(250), [ErisaLACity]) AS [ErisaLACity], Convert(VarChar(250), [ErisaLAState]) AS [ErisaLAState], Convert(VarChar(250), [ErisaLAZip]) AS [ErisaLAZip], Convert(VarChar(250), [ErisaLAPhone]) AS [ErisaLAPhone], Convert(VarChar(250), [ERISAComments]) AS [ERISAComments], Convert(VarChar(250), [CSNApproval]) AS [CSNApproval], Convert(VarChar(250), [CSNPPO]) AS [CSNPPO], Convert(VarChar(250), [CSNOAP]) AS [CSNOAP], Convert(VarChar(250), [CSNSupplemental]) AS [CSNSupplemental], Convert(VarChar(250), [CSNReplacement]) AS [CSNReplacement], Convert(VarChar(250), [CSNType]) AS [CSNType], Convert(VarChar(250), [CSNNBNIL915]) AS [CSNNBNIL915], Convert(VarChar(250), [CSNNBNPA910]) AS [CSNNBNPA910], Convert(VarChar(250), [CSNNBNPA911]) AS [CSNNBNPA911], Convert(VarChar(250), [CSNNBNFL918]) AS [CSNNBNFL918], Convert(VarChar(250), [CSNNBNCA915]) AS [CSNNBNCA915], Convert(VarChar(250), [CSNNBNMA904]) AS [CSNNBNMA904], Convert(VarChar(250), [CSNOwnLogo]) AS [CSNOwnLogo], Convert(VarChar(250), [CSNNBNAllBranches]) AS [CSNNBNAllBranches], Convert(VarChar(250), [CSNNBNBranchComment]) AS [CSNNBNBranchComment], Convert(VarChar(250), [ASOStopLoss]) AS [ASOStopLoss], Convert(VarChar(250), [ASOCigna]) AS [ASOCigna], Convert(VarChar(250), [ASOThirdParty]) AS [ASOThirdParty], Convert(VarChar(250), [ASORetirees]) AS [ASORetirees], Convert(VarChar(250), [ISOStopLoss]) AS [ISOStopLoss], Convert(VarChar(250), [ISOCigna]) AS [ISOCigna], Convert(VarChar(250), [ISOThirdParty]) AS [ISOThirdParty], Convert(VarChar(250), [ISORetirees]) AS [ISORetirees], Convert(VarChar(250), [ASOReimbCigna]) AS [ASOReimbCigna], Convert(VarChar(250), [ASOReimbOther]) AS [ASOReimbOther]' 

;with cte as (
       Select *,Grp = sum(len(RetVal)-0) over (Order By RetSeq)/950 
       From [dbo].[udf-Str-Parse-8K](replace(@S,'],','],|'),'|') 
) 
Select S = replace(Stuff((Select '' +String + '|||' 
    From (
      Select Grp 
        ,String = (Select Stuff((Select ' '+RetVal From cte Where Grp=A.Grp For XML Path ('')),1,1,'')) 
      From (Select Distinct Grp From cte) A 
     ) A 
    Order by Grp 
    For XML Path ('')),1,0,'') 
    ,'|||',char(13)) 

返回

TransID, Convert(VarChar(250), [TPSYN]) AS [TPSYN], Convert(VarChar(250), [SureFitYN]) AS [SureFitYN], Convert(VarChar(250), [AccountNbr]) AS [AccountNbr], Convert(VarChar(250), [Account Name]) AS [Account Name], Convert(VarChar(250), [EffectiveDate]) AS [EffectiveDate], Convert(VarChar(250), [IM]) AS [IM], Convert(VarChar(250), [RevisionDate]) AS [RevisionDate], Convert(VarChar(250), [AccountAddress]) AS [AccountAddress], Convert(VarChar(250), [LSOCode]) AS [LSOCode], Convert(VarChar(250), [ASONonASOBoth]) AS [ASONonASOBoth], Convert(VarChar(250), [MedicalYN]) AS [MedicalYN], Convert(VarChar(250), [NonManagedCareYN]) AS [NonManagedCareYN], Convert(VarChar(250), [ManagedCareYN]) AS [ManagedCareYN], Convert(VarChar(250), [HRAYN]) AS [HRAYN], Convert(VarChar(250), [MedHRAYN]) AS [MedHRAYN], Convert(VarChar(250), [LPHRAYN]) AS [LPHRAYN], Convert(VarChar(250), [HealthyFutYN]) AS [HealthyFutYN], Convert(VarChar(250), [HAHRAYN]) AS [HAHRAYN], 
Convert(VarChar(250), [HSAYN]) AS [HSAYN], Convert(VarChar(250), [FSAHealthCare]) AS [FSAHealthCare], Convert(VarChar(250), [FSADependentCare]) AS [FSADependentCare], Convert(VarChar(250), [DentalYN]) AS [DentalYN], Convert(VarChar(250), [TaftHartley]) AS [TaftHartley], Convert(VarChar(250), [ParameterComments]) AS [ParameterComments], Convert(VarChar(250), [MLRAvgLivesNumber]) AS [MLRAvgLivesNumber], Convert(VarChar(250), [MLRAvgLivesRptYear]) AS [MLRAvgLivesRptYear], Convert(VarChar(250), [MLRContact]) AS [MLRContact], Convert(VarChar(250), [MLRContactAddress1]) AS [MLRContactAddress1], Convert(VarChar(250), [MLRContactAddress2]) AS [MLRContactAddress2], Convert(VarChar(250), [MLRContactCity]) AS [MLRContactCity], Convert(VarChar(250), [MLRContactState]) AS [MLRContactState], Convert(VarChar(250), [MLRContactZip]) AS [MLRContactZip], Convert(VarChar(250), [HealthRiskAssessment]) AS [HealthRiskAssessment], Convert(VarChar(250), [ErisaYN]) AS [ErisaYN], 
Convert(VarChar(250), [ErisaPlanName]) AS [ErisaPlanName], Convert(VarChar(250), [ErisaPlanNumber]) AS [ErisaPlanNumber], Convert(VarChar(250), [ErisaPlanEndsMM]) AS [ErisaPlanEndsMM], Convert(VarChar(250), [ErisaPlanEndsDD]) AS [ErisaPlanEndsDD], Convert(VarChar(250), [ErisaAdministrator]) AS [ErisaAdministrator], Convert(VarChar(250), [ErisaClaimAdministrator]) AS [ErisaClaimAdministrator], Convert(VarChar(250), [ErisaCost]) AS [ErisaCost], Convert(VarChar(250), [ErisaEOB]) AS [ErisaEOB], Convert(VarChar(250), [ErisaEOBName]) AS [ErisaEOBName], Convert(VarChar(250), [ErisaEOBAddress1]) AS [ErisaEOBAddress1], Convert(VarChar(250), [ErisaEOBAddress2]) AS [ErisaEOBAddress2], Convert(VarChar(250), [ErisaEOBCity]) AS [ErisaEOBCity], Convert(VarChar(250), [ErisaEOBState]) AS [ErisaEOBState], Convert(VarChar(250), [ErisaEOBZip]) AS [ErisaEOBZip], Convert(VarChar(250), [ErisaEOBPhone]) AS [ErisaEOBPhone], Convert(VarChar(250), [ErisaPAName]) AS [ErisaPAName], 
Convert(VarChar(250), [ErisaPAAddress1]) AS [ErisaPAAddress1], Convert(VarChar(250), [ErisaPAAddress2]) AS [ErisaPAAddress2], Convert(VarChar(250), [ErisaPACity]) AS [ErisaPACity], Convert(VarChar(250), [ErisaPAState]) AS [ErisaPAState], Convert(VarChar(250), [ErisaPAZip]) AS [ErisaPAZip], Convert(VarChar(250), [ErisaPAPhone]) AS [ErisaPAPhone], Convert(VarChar(250), [ErisaLAName]) AS [ErisaLAName], Convert(VarChar(250), [ErisaLAAddress1]) AS [ErisaLAAddress1], Convert(VarChar(250), [ErisaLAAddress2]) AS [ErisaLAAddress2], Convert(VarChar(250), [ErisaLACity]) AS [ErisaLACity], Convert(VarChar(250), [ErisaLAState]) AS [ErisaLAState], Convert(VarChar(250), [ErisaLAZip]) AS [ErisaLAZip], Convert(VarChar(250), [ErisaLAPhone]) AS [ErisaLAPhone], Convert(VarChar(250), [ERISAComments]) AS [ERISAComments], Convert(VarChar(250), [CSNApproval]) AS [CSNApproval], Convert(VarChar(250), [CSNPPO]) AS [CSNPPO], Convert(VarChar(250), [CSNOAP]) AS [CSNOAP], 
Convert(VarChar(250), [CSNSupplemental]) AS [CSNSupplemental], Convert(VarChar(250), [CSNReplacement]) AS [CSNReplacement], Convert(VarChar(250), [CSNType]) AS [CSNType], Convert(VarChar(250), [CSNNBNIL915]) AS [CSNNBNIL915], Convert(VarChar(250), [CSNNBNPA910]) AS [CSNNBNPA910], Convert(VarChar(250), [CSNNBNPA911]) AS [CSNNBNPA911], Convert(VarChar(250), [CSNNBNFL918]) AS [CSNNBNFL918], Convert(VarChar(250), [CSNNBNCA915]) AS [CSNNBNCA915], Convert(VarChar(250), [CSNNBNMA904]) AS [CSNNBNMA904], Convert(VarChar(250), [CSNOwnLogo]) AS [CSNOwnLogo], Convert(VarChar(250), [CSNNBNAllBranches]) AS [CSNNBNAllBranches], Convert(VarChar(250), [CSNNBNBranchComment]) AS [CSNNBNBranchComment], Convert(VarChar(250), [ASOStopLoss]) AS [ASOStopLoss], Convert(VarChar(250), [ASOCigna]) AS [ASOCigna], Convert(VarChar(250), [ASOThirdParty]) AS [ASOThirdParty], Convert(VarChar(250), [ASORetirees]) AS [ASORetirees], Convert(VarChar(250), [ISOStopLoss]) AS [ISOStopLoss], 
Convert(VarChar(250), [ISOCigna]) AS [ISOCigna], Convert(VarChar(250), [ISOThirdParty]) AS [ISOThirdParty], Convert(VarChar(250), [ISORetirees]) AS [ISORetirees], Convert(VarChar(250), [ASOReimbCigna]) AS [ASOReimbCigna], Convert(VarChar(250), [ASOReimbOther]) AS [ASOReimbOther] 

解析UDF - - 如果需要,可以爲INLINE

CREATE FUNCTION [dbo].[udf-Str-Parse-8K] (@String varchar(max),@Delimiter varchar(25)) 
Returns Table 
As 
Return ( 
    with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)), 
      cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 a,cte1 b,cte1 c,cte1 d) A), 
      cte3(N) As (Select 1 Union All Select t.N+DataLength(@Delimiter) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter)) = @Delimiter), 
      cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter,@String,s.N),0)-S.N,8000) From cte3 S) 

    Select RetSeq = Row_Number() over (Order By A.N) 
      ,RetVal = LTrim(RTrim(Substring(@String, A.N, A.L))) 
    From cte4 A 
); 
--Orginal Source http://www.sqlservercentral.com/articles/Tally+Table/72993/ 
--Select * from [dbo].[udf-Str-Parse-8K]('Dog,Cat,House,Car',',') 
--Select * from [dbo].[udf-Str-Parse-8K]('John||Cappelletti||was||here','||') 

編輯 - INLINE Verserion(無需對UDF)

;with cte as (
       Select *,Grp = sum(len(RetVal)-0) over (Order By RetSeq)/950 
       From (
         Select RetSeq = Row_Number() over (Order By (Select null)) 
           ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)'))) 
         From (Select x = Cast('<x>' + replace((Select replace(replace(@S,'],','],|'),'|','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
         Cross Apply x.nodes('x') AS B(i) 
         ) A 
) 
Select S = replace(Stuff((Select '' +String + '|||' 
    From (
      Select Grp 
        ,String = (Select Stuff((Select ' '+RetVal From cte Where Grp=A.Grp For XML Path ('')),1,1,'')) 
      From (Select Distinct Grp From cte) A 
     ) A 
    Order by Grp 
    For XML Path ('')),1,0,'') 
    ,'|||',char(13)) 
+0

我不知道我是否沒有正確運行或理解輸出,但是當我將輸出粘貼到Textpad中時,我將所有內容都放在一行上。根本沒有換行符。我需要大約每950個字符換行,具體取決於最接近的「],」實例的位置。 –

+0

我剛剛更新了問題,以顯示結果字符串應該是什麼樣子。 –

+0

FRICKIN'BRILLIANT!我使用了Inline版本,並且它是現貨。 謝謝! –