SQL使用SUBSTRING和CHARINDEX减去字符串

时间:2014-12-17 20:59:10

标签: sql

我有以下字符串:

'{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com'

我想只提取MessageID部分:

<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>

我尝试过使用substring和charindex函数失败了:

   SELECT SUBSTRING('{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com',
   (SELECT CHARINDEX(' Message-ID=<','{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com')),
   (SELECT CHARINDEX('>, Authentication-Results', '{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com')))

我的查询结果是:

 Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com

我做错了什么?

3 个答案:

答案 0 :(得分:1)

SUBSTRING的第三个参数是LENGTH。你的陈述正在传递角色......

SUBSTRING ( expression ,start , length )

试试这个:

SELECT SUBSTRING('{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com',
(SELECT CHARINDEX(' Message-ID=<','{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com')),
CHARINDEX('>, Authentication-Results', '{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com')
-
CHARINDEX(' Message-ID=<','{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com') 
+1)

(在您的示例中,值应为75)

答案 1 :(得分:1)

这是Charindex功能。请参阅http://msdn.microsoft.com/en-us/library/ms186323.aspx。 Charindex得到了角色的第一个出现率索引,所以

   select CHARINDEX('A','TESTING A LETTER')

上面将返回9,如果你做一个子字符串,子字符串将从9开始,而不是从字符串的结尾开始。

对于您的查询,解决方案如下;

  DECLARE @String varchar(500) = '{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com'

  SELECT SUBSTRING(@String,
   CHARINDEX('Message-ID=<',@String) + 12,
   CHARINDEX('>',@String,CHARINDEX('Message-ID=<',@String)))

答案 2 :(得分:0)

试试这个: 添加REPLACE:

SELECT REPLACE(SUBSTRING('{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification- 
Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-a32410de78@abd.local.test.global>, 
Authentication-Results=vadmzmail2342.test.com', (SELECT CHARINDEX(' Message-ID=<','{Mapi-Reply-
Recipient-Smtp-Proxies=, Mapi-Non-Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-
4345c-b232429-a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com')),
(SELECT CHARINDEX('>, Authentication-Results', '{Mapi-Reply-Recipient-Smtp-Proxies=, Mapi-Non-
Receipt-Notification-Requested=true, Message-ID=<6a2k4081-f6134-4345c-b232429-
a32410de78@abd.local.test.global>, Authentication-Results=vadmzmail2342.test.com'))) ,' Message-ID=  
<','')