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);
这篇关于如何搜索字符串并仅返回数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何搜索字符串并仅返回数值?
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
