剥离字符串

时间:2014-03-07 06:02:45

标签: sql sql-server

我们如何删除下面的字符串

*** MAXWELLL (Maxwell Lee) *** December 27, 2006 11:36:00  NPF  $36.90 from PO eff. Nov 1st 2006 $38.10 $38.90  Criterion CL $28.90   *** MAXWELLL (Maxwell Lee) *** February 6, 2007 13:11:00  NPF 36.90 no change, less 1.5% ins, less 2% pay disc, less 1.30 ocean ++  GLD 30.90 (fr 28.90), less 1.5% ins, less 2% pay disc, less 1.26 ocean ++     *** MAXWELLL (Maxwell Lee) *** October 4, 2007 10:13:00  Per ML price decrease effective 08/01/07. CL from $ 30.90 to $28.90 NPF from $36.90 to $37.90 due to Tillotson   ** MAXWELLL (Maxwell Lee) ***  May 29, 2007 at  1:02pm  2006 Spiff $11,883 2007 Spiff $13,700.    *** MAXWELLL (Maxwell Lee) *** December 6, 2007 10:51:00  Price increase in Criterion CL 3/6/2008 See e-mail to Jeff Sherman CL from $28.90 to $30.15 or $30.40     *** MAXWELLL (Maxwell Lee) *** March 31, 2008 16:01:00  3/6/08 CL $31.50,  5/1/08 NPF $38.95     *** CYNTHIAH (Cynthia Hernandez) *** May 8, 2008 09:51:00  Per ML Remind him when HS stops buying from Adenna about insurance.  Do we need 3 mil insurance. Possible solution:  Sell thru Dermatec on LTL biz.    *** MAXWELLL (Maxwell Lee) *** August 29, 2008 11:05:00  See HS Container cost calc excel for most current 40' pricing.    *** MAXWELLL (Maxwell Lee) *** December 17, 2008 16:17:00  Price change - 60 days notice in advanced, multiple times.     *** EVANGC (Evangelene Cheng) *** February 17, 2012 14:02:00  JES NOTE:  COMMENT:  HESC01: When shipping Adenna product 84+ CS. HESC02:  Container Orders HESC03:  When shippign Adenna Product less than 84 CS; (Ship-Collect).      *** MASTER (Supervisor) *** March 5, 2014 02:32:11 ~~deleted=1  Test  *** MASTER (Supervisor) *** March 5, 2014 03:06:33 ~~deleted=1  sdfdf  *** MASTER (Supervisor) *** March 5, 2014 20:36:05 ~~deleted=1  Test  *** MASTER (Supervisor) *** March 5, 2014 20:43:07 ~~deleted=1  Test Note  

采用以下格式

1st Column : String between *** , only first occurrences and neglect others 
2nd Column : Date and Time
3rd Column : Remaining String

请帮忙。

2 个答案:

答案 0 :(得分:0)

我对这些功能并不是很有经验,但这就是我提出的所有内容:

DECLARE @MyString as varchar(500)
SET @MyString='*** MIGUELP (Miguel Padilla) ***  August 14, 2012 12:51:33  08/14/2012 -  Always email PRO# the next day the order ships.'
DECLARE @RevString as varchar(500)
SET @RevString=REVERSE(@MyString)

SELECT Substring(@MyString,5,charindex(' *',@MyString)-4) as FirstCol
,SUBSTRING(@MyString,charindex(' *** ',@MyString)+4,38) as DateTime
,Reverse(substring(@RevString,1,charindex('-',@RevString)-1)) as Remaining

我相信,会有一种更好,更有效的方式。

请参阅SQL Fiddle

答案 1 :(得分:0)

在这个不知道你想要支持的所有边缘情况的情况下,给你一个直接答案真的很难,但是这里有一些我用来从像你这样的伪分隔日志消息中提取数据的技术。

编辑:更新字符串后的修改策略。对于第二个分隔符,我现在使用结束时间(11:36:00)查找第一个“:NN”字符集。嘿,它不一定非常漂亮:)。

declare @s varchar(1000);
set @s = '*** MAXWELLL (Maxwell Lee) *** December 27, 2006 11:36:00  NPF  $36.90 from PO eff. Nov 1st 2006 $38.10 $38.90  Criterion CL $28.90   *** MAXWELLL (Maxwell Lee) *** February 6, 2007 13:11:00  NPF 36.90 no change, less 1.5% ins, less 2% pay disc, less 1.30 ocean ++  GLD 30.90 (fr 28.90), less 1.5% ins, less 2% pay disc, less 1.26 ocean ++     *** MAXWELLL (Maxwell Lee) *** October 4, 2007 10:13:00  Per ML price decrease effective 08/01/07. CL from $ 30.90 to $28.90 NPF from $36.90 to $37.90 due to Tillotson   ** MAXWELLL (Maxwell Lee) ***  May 29, 2007 at  1:02pm  2006 Spiff $11,883 2007 Spiff $13,700.    *** MAXWELLL (Maxwell Lee) *** December 6, 2007 10:51:00  Price increase in Criterion CL 3/6/2008 See e-mail to Jeff Sherman CL from $28.90 to $30.15 or $30.40     *** MAXWELLL (Maxwell Lee) *** March 31, 2008 16:01:00  3/6/08 CL $31.50,  5/1/08 NPF $38.95     *** CYNTHIAH (Cynthia Hernandez) *** May 8, 2008 09:51:00  Per ML Remind him when HS stops buying from Adenna about insurance.  Do we need 3 mil insurance. Possible solution:  Sell thru Dermatec on LTL biz.    *** MAXWELLL (Maxwell Lee) *** August 29, 2008 11:05:00  See HS Container cost calc excel for most current 40'' pricing.    *** MAXWELLL (Maxwell Lee) *** December 17, 2008 16:17:00  Price change - 60 days notice in advanced, multiple times.     *** EVANGC (Evangelene Cheng) *** February 17, 2012 14:02:00  JES NOTE:  COMMENT:  HESC01: When shipping Adenna product 84+ CS. HESC02:  Container Orders HESC03:  When shippign Adenna Product less than 84 CS; (Ship-Collect).      *** MASTER (Supervisor) *** March 5, 2014 02:32:11 ~~deleted=1  Test  *** MASTER (Supervisor) *** March 5, 2014 03:06:33 ~~deleted=1  sdfdf  *** MASTER (Supervisor) *** March 5, 2014 20:36:05 ~~deleted=1  Test  *** MASTER (Supervisor) *** March 5, 2014 20:43:07 ~~deleted=1  Test Note'

;with x (doc) as
(
    select cast('<r>'+replace(stuff(@s, patindex('%:[0-9][0-9] %',@s)+3, 0, '***'), '***', '</r><r>')+'</r>' as xml)
)
select  [1st]=ltrim(rtrim(x.doc.query('data(r[position()=2])').value('.', 'varchar(100)'))),
        [2nd]=ltrim(rtrim(x.doc.query('data(r[position()=3])').value('.', 'varchar(100)'))),
        [3rd]=ltrim(rtrim(x.doc.query('data(r[position()=4])').value('.', 'varchar(100)')))
from x

好的,我误解了。我以为你只想留到下一个入口。 下面可能更干净,不会截断“剩余”列。查看列a,b,c并查看其工作原理。你应该可以和他们一起玩来调整你的解析。如果你想迭代这些条目,你需要在循环中进行这种解析:

;with c(a,b,c) as
(
            -- 1st ***
    select  charindex('***', @s, 0)+3,
            -- 2nd ***
            charindex('***', @s, 4),
            --the end of the time string
            patindex('%:[0-9][0-9] %',@s)+3 
)
select  [1st]=ltrim(rtrim(substring(@s, a, b-a))), 
        [2nd]=ltrim(rtrim(substring(@s, a+b, c-(a+b)))),
        [3rd]=ltrim(rtrim(substring(@s, c, len(@s)))),
        *
from    c;