从列中选择特定字符串

时间:2017-03-07 19:34:16

标签: tsql

我的表中的数据如下所示:

select email_body from email_table


email_body
----------
Ashely, call me.   thanks  --- Original message ---  From: Ashley  To: Lee Subject: Homework  Sent: 3/6/2017 2:32:54 AM GMT  I have a quick question.
Mike, I have all the data you need.  Jim   --- Original message ---  From: Mike  To: Jim Subject: Not Sure  Sent: 3/18/2017 12:02:51 AM GMT  Are you available to go over this?
William, Are you around.   thanks  --- Original message ---  From: Joe  To: William Subject: Nothing much  Sent: 4/16/2017 4:17:23 PM GMT  I need some sleep.
Joan, call me.   Ralph  --- Original message ---  From: Ralph  To: Joan Subject: I need help  Sent: 3/30/2017 5:12:50 AM GMT  Call Rich.

我想返回email_body中列出的日期和时间:

Results:

Original_message
----------------
3/6/2017 2:32:54 AM
3/18/2017 12:02:51 AM
4/16/2017 4:17:23 PM
3/30/2017 5:12:50 AM

3 个答案:

答案 0 :(得分:3)

因为日期长度可能会有所不同(例如1/1/201712/12/2017), 你需要找到它的起始位置(很容易 - Sent:之后的6个字符)和它结束的位置(在M GMT之前是2个字符)。 剩下的工作是substring完成的。

SELECT SUBSTRING(email_body, CHARINDEX('Sent:', email_body) + 6,
 CHARINDEX('M GMT ',email_body) - 2 - CHARINDEX('Sent:', email_body) - 6) as Original_message
FROM email_table

答案 1 :(得分:1)

您可以使用类似

的内容
select SUBSTRING (email_body , PATINDEX ( 'Subject' ,email_body )+1, 18) from email_table

答案 2 :(得分:0)

如果您对Table-Valued-Function开放,它将根据模式 AND 快速安全地提取一个或多个值,您会厌倦所有必需的字符串操作,请考虑以下内容:

示例

Declare @email_table table (id int,email_body varchar(max))
Insert Into @email_table values
(1,'Ashely, call me.   thanks  --- Original message ---  From: Ashley  To: Lee Subject: Homework  Sent: 3/6/2017 2:32:54 AM GMT  I have a quick question.'),
(2,'Mike, I have all the data you need.  Jim   --- Original message ---  From: Mike  To: Jim Subject: Not Sure  Sent: 3/18/2017 12:02:51 AM GMT  Are you available to go over this?'),
(3,'William, Are you around.   thanks  --- Original message ---  From: Joe  To: William Subject: Nothing much  Sent: 4/16/2017 4:17:23 PM GMT  I need some sleep.'),
(4,'Joan, call me.   Ralph  --- Original message ---  From: Ralph  To: Joan Subject: I need help which was  Sent: 3/30/2017 5:12:50 AM GMT  Call Rich.')

Select A.ID
      ,Original_message = B.RetVal
 From  @email_table A
 Cross Apply [dbo].[udf-Str-Extract](A.email_body,' Sent: ',' GMT ') B

<强>返回

ID  Original_message
1   3/6/2017 2:32:54 AM
2   3/18/2017 12:02:51 AM
3   4/16/2017 4:17:23 PM
4   3/30/2017 5:12:50 AM

实际的TVF返回

RetSeq  RetPos  RetLen  RetVal
1       101     19      3/6/2017 2:32:54 AM
1       115     21      3/18/2017 12:02:51 AM
1       114     20      4/16/2017 4:17:23 PM
1       111     20      3/30/2017 5:12:50 AM

感兴趣的UDF

CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
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 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
       cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
       cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)

Select RetSeq = Row_Number() over (Order By N)
      ,RetPos = N
      ,RetLen = charindex(@Delimiter2,RetVal)-1
      ,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1)
 From (Select A.N,RetVal = ltrim(rtrim(Substring(@String, A.N, A.L))) From cte4 A ) A
 Where charindex(@Delimiter2,RetVal)>1
)
/*
Max Length of String 1MM characters

Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/