[英]SQL substract string using SUBSTRING and CHARINDEX
我有以下字符串:
'{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>
我尝试使用子字符串和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
我做错了什么?
SUBSTRING的第三个参数是长度。 您的陈述正在传递角色位置...
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)
它是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)))
尝试以下操作:添加替换:
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=
<','')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.