将一个字段拆分为多个注释 - SQL

时间:2018-03-18 18:36:35

标签: sql

我有一个表在一个字段中有多个注释。 例如,

  

1)索赔重新制作Rebecca Byrd 1/17/2018 3:17:53 PM PER CHGS,我们需要   修改此索赔Rebecca Byrd 1/10/2018 1:55:37 PM等待回家   计划回复Rebecca Byrd 1/2/2018 1:58:31 PM A / R SENT TO CHGS ON   这个索赔。拒绝作为重复,但只有一个索赔   这个DOS的ILINKBLUE。测试Byrd 12/29/2017 6:34:36 AM

     

2)HCRR账户Sheila Johnson 9/28/2017 7:37:55 AM

     

3)联系VA VISN与玛丽谈话,她表示已经完成了18.32美元   06/27/2014 - #675678-她给了我国库#86-72-1141。一世   联系TEST并与TEST交谈 - 我给她的号码,日期和时间 -   她在2014年6月27日发现了ck和pmt为0.00美元 - 她说是的   在blah bank下兑现 - 追踪#0jgdjgkd。测试测试7/28/2017 1:21:11   PM

我想将它分成不同的行,如下

1) CLAIM REFILED Rebecca Byrd 1/17/2018 3:17:53 PM 
2) PER BCBS, WE NEED TO REFILE THIS CLAIM Rebecca Byrd 1/10/2018 1:55:37 PM 
3) WAITING ON HOME PLAN TO REPLY Rebecca Byrd 1/2/2018 1:58:31 PM 
4) A/R SENT TO BCBS ON THIS CLAIM. DENIED AS A DUPLICATE, BUT THERE WAS ONLY ONE CLAIM IN ILINKBLUE FOR THIS DOS. Rebecca Byrd 12/29/2017 6:34:36 AM 

所有评论都将以日期结束。所以我认为我可以使用AM或PM作为分隔符。但难以分开它。

1 个答案:

答案 0 :(得分:0)

首先:这个设计很糟糕你应该 - 如果有机会 -  真的改变了!使用此方法将所有注释转移到相关的边表中。

你的问题不是很清楚,但是我的魔法水晶球心情很好并告诉我,你可能正在寻找这个:

CREATE FUNCTION dbo.SplitCommentOnTime(@str VARCHAR(MAX))
RETURNS TABLE
AS
RETURN
WITH recCTE AS
(
    SELECT 1 AS Pos
          ,LEFT(@str,PATINDEX('%:[0-5][0-9] [AP]M %',@str + ' ')+5) AS Part
          ,SUBSTRING(@str,PATINDEX('%:[0-5][0-9] [AP]M %',@str + ' ')+7,LEN(@str)) AS Remainder

    UNION ALL
    SELECT Pos+1
          ,LEFT(Remainder,PATINDEX('%:[0-5][0-9] [AP]M %',Remainder + ' ')+5) 
          ,SUBSTRING(Remainder,PATINDEX('%:[0-5][0-9] [AP]M %',Remainder + ' ')+7,LEN(@str)) 
    FROM recCTE
    WHERE PATINDEX('%:[0-5][0-9] [AP]M %',Remainder + ' ')>0
)
SELECT Pos,Part 
FROM recCTE;
GO

DECLARE @tbl TABLE(ID INT IDENTITY,Comment VARCHAR(MAX));
INSERT INTO @tbl VALUES
 ('CLAIM REFILED Rebecca Byrd 1/17/2018 3:17:53 PM PER CHGS, WE NEED TO REFILE THIS CLAIM Rebecca Byrd 1/10/2018 1:55:37 PM WAITING ON HOME PLAN TO REPLY Rebecca Byrd 1/2/2018 1:58:31 PM A/R SENT TO CHGS ON THIS CLAIM. DENIED AS A DUPLICATE, BUT THERE WAS ONLY ONE CLAIM IN ILINKBLUE FOR THIS DOS. Test Byrd 12/29/2017 6:34:36 AM')
,('HCRR ACCOUNT Sheila Johnson 9/28/2017 7:37:55 AM')
,('Contacted VA VISN spoke with Mary she stated pmt $18.32 was made on 06/27/2014 - #675678- she gave me treasury uh# 86-72-1141. I contacted TEST and spoke with TEST - i gave her number, date and pmt - she found ck and pmt of $0.00 for date 06/27/2014- she said it was cashed under blah bank - trace #0jgdjgkd. Test Test 7/28/2017 1:21:11 PM')

SELECT *
FROM @tbl 
OUTER APPLY dbo.SplitCommentOnTime(Comment); 


GO
DROP FUNCTION dbo.SplitCommentOnTime;

结果

ID  Pos Part
1   1   CLAIM REFILED Rebecca Byrd 1/17/2018 3:17:53 PM
1   2   PER CHGS, WE NEED TO REFILE THIS CLAIM Rebecca Byrd 1/10/2018 1:55:37 PM
1   3   WAITING ON HOME PLAN TO REPLY Rebecca Byrd 1/2/2018 1:58:31 PM
1   4   A/R SENT TO CHGS ON THIS CLAIM. DENIED AS A DUPLICATE, BUT THERE WAS ONLY ONE CLAIM IN ILINKBLUE FOR THIS DOS. Test Byrd 12/29/2017 6:34:36 AM
2   1   HCRR ACCOUNT Sheila Johnson 9/28/2017 7:37:55 AM
3   1   Contacted VA VISN spoke with Mary she stated pmt $18.32 was made on 06/27/2014 - #675678- she gave me treasury uh# 86-72-1141. I contacted TEST and spoke with TEST - i gave her number, date and pmt - she found ck and pmt of $0.00 for date 06/27/2014- she said it was cashed under blah bank - trace #0jgdjgkd. Test Test 7/28/2017 1:21:11 PM

一些解释

该函数使用递归CTE跳过字符串寻找模式。模式是%:[0-5][0-9] [AP]M %。这意味着:一个双点,然后是0-5中的一个数字,然后是0-9的数字,一个空白,A或P,然后是M和一个空白。

其余为LEFTSUBSTRING

提示

分割这些字符串怪物后,您可以使用REVERT()并搜索第三个空白。获取此片段,再次还原,并将其转换为真实的DATETIME。通过这种方式,您可以获得具有良好查询性能的边桌。

你可能会以同样的方式得到评论者......