在两个已知字符串之间选择一个字符串的 SQL 查询

编程入门 行业动态 更新时间:2024-10-24 06:37:34
本文介绍了在两个已知字符串之间选择一个字符串的 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个 SQL 查询来获取两个已知字符串之间的值(返回的值应该以这两个字符串开头和结尾).

I need a SQL query to get the value between two known strings (the returned value should start and end with these two strings).

一个例子.

我只知道这只狗很坏,不管其他人怎么想,都要立即受到严厉的惩罚."

"All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what anyone else thought."

在这种情况下,已知字符串是狗"和立即".所以我的查询应该返回这只狗很坏,需要立即严厉惩罚"

In this case the known strings are "the dog" and "immediately". So my query should return "the dog had been very bad and required harsh punishment immediately"

到目前为止我已经想出了这个但无济于事:

I've come up with this so far but to no avail:

SELECT SUBSTRING(@Text, CHARINDEX('the dog', @Text), CHARINDEX('immediately', @Text))

@Text 是包含主字符串的变量.

@Text being the variable containing the main string.

有人可以帮我解决哪里出错了吗?

Can someone please help me with where I'm going wrong?

推荐答案

问题是子字符串参数的第二部分包含第一个索引.您需要从第二个索引中减去第一个索引才能完成这项工作.

The problem is that the second part of your substring argument is including the first index. You need to subtract the first index from your second index to make this work.

SELECT SUBSTRING(@Text, CHARINDEX('the dog', @Text) , CHARINDEX('immediately',@text) - CHARINDEX('the dog', @Text) + Len('immediately'))

更多推荐

在两个已知字符串之间选择一个字符串的 SQL 查询

本文发布于:2023-11-02 17:43:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   两个   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!