用什么来从字符串中获取DOC?

时间:2017-01-11 06:32:12

标签: sql-server

我必须通过sql查询获取一个子字符串,该查询应该从我的db记录中获取“DOC =某个数字”。 我已经尝试了所有的函数,如charIndex,substring但我没有得到我所需的输出。

输出应该是;

Doc = some_number(22333); DOC = some_num(333) 。 。

.
select * from SMSReceived
where ReceivedId Between 29 and 40707;;


SELECT top 20 t.ReceivedId, SUBSTRING(s.MessageText,9,13) some_name 
FROM SMSReceived t inner join SMSReceived s on t.ReceivedId=s.ReceivedId
where s.ReceivedId Between 29 and 40707;

这是下面屏幕截图的查询;

Query Result

1 个答案:

答案 0 :(得分:0)

PATINDEX提供了指定字符串的第一次出现。

    SELECT top 20 t.ReceivedId, SUBSTRING(s.MessageText,PATINDEX('%DOC%',s.MessageText),13) 
some_name 
    FROM SMSReceived t inner join SMSReceived s on t.ReceivedId=s.ReceivedId
    where s.ReceivedId Between 29 and 40707;