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);
这篇关于如何搜索字符串并仅返回数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何搜索字符串并仅返回数值?
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
