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);
这篇关于如何搜索字符串并仅返回数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何搜索字符串并仅返回数值?
基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
