如何搜索字符串并仅返回数值?

How to search a string and return only numeric value?(如何搜索字符串并仅返回数值?)
本文介绍了如何搜索字符串并仅返回数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要创建一个公式列.在此列中,我需要返回一个数值.

I need to create a formula column. In this column I need to return a numeric value.

示例:

database.dbo.table1 有一个名为message"的列,其中包含一条长消息.

database.dbo.table1 has a column called "message" that contains a long message.

消息的格式如下:各种单词、字符、空格<this document is>文档# = 12345 <这个文档是>

我需要做的是搜索消息,找到this document is"并在这两个短语之间搜索文档的数值,在公式列中返回文档#.

What I need to do is search through the message, find "this document is" and searched between both of those phrases for the numeric value of the document, return the document # inside the formula column.

推荐答案

使用 SQLXML XQuery 函数/方法(例如 doc.value() 通常很昂贵,应尽可能避免.在这种情况下,根据提供的信息,您可以使用 CHARINDEX.

Using SQLXML XQuery functions/methods (e.g. doc.value() is generally expensive and should be avoided when possible. In this case, based on the information provided, you can get what you need using CHARINDEX.

如果您正在使用 (n)varchar 字段,您可以这样做:

If you are working with a (n)varchar field you could do this:

declare @mytab table (doc varchar(max))    
insert into @mytab values ('<SendDocument DocumentID="1234567">true</SendDocument>');

SELECT SUBSTRING(ci,1,CHARINDEX('"',ci)-1)
FROM (SELECT SUBSTRING(doc, CHARINDEX('DocumentID="',doc)+12,20) FROM @mytab) start(ci);

如果您正在使用 XML 字段,您可以这样做:

If you are working with an XML field you could do this:

declare @mytab table (doc xml); 
insert into @mytab values ('<SendDocument DocumentID="1234567">true</SendDocument>');

SELECT SUBSTRING(ci,1,CHARINDEX('"',ci)-1)
FROM 
(
  SELECT SUBSTRING
    (
      CAST(doc AS varchar(max)), 
      CHARINDEX('DocumentID="', CAST(doc AS varchar(max)))+12,
      20
    )
  FROM @mytab
) start(ci);

这篇关于如何搜索字符串并仅返回数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)