sql - 从开始和结束字符相同的字符串中检索文本
问题描述
考虑到开始和结束是相同的字符串值,如何获取****星之间的文本?
Create Table #temp
(
TestString varchar(400)
)
insert into #temp
(
TestString
)
select
'**** mary had a little lamb****'
union
select
'**** humpy dumpty had a great fall**** All the king''s horses and all the king''s men'
解决方案
您可以按如下方式使用基本字符串函数:
SELECT
TestString,
SUBSTRING(TestString,
CHARINDEX('****', TestString) + 4,
CHARINDEX('****', TestString, CHARINDEX('****', TestString) + 1) - 5) AS contents
FROM yourTable;
演示
数据:
WITH yourTable AS (
SELECT '**** mary had a little lamb****' AS TestString UNION ALL
SELECT '**** humpy dumpty had a great fall**** All the king''s horses and all the king''s men'
)
推荐阅读
- java - Hibernate Validator @SafeHtml 无法允许自定义 HTML 标记和属性
- c# - 过滤器无法正常工作 - 当内部连接表时
- c# - Chilkat 如何使用 Bash 终端的参数启动 SSH
- solr - Solr 搜索是否可以在键中包含通配符?
- objective-c - ARKit 深度图像素缓冲区中的黑色像素是什么?
- google-search-console - iframe 视频加载图像中缺少字段“thumbnailUrl”
- java - Spring MVC Hibernate没有xml使用DriverManagerDataSource如何映射属性
- c# - 起订量设置抛出异常
- php - 多循环中的查询和按最后一个查询的 id 排序
- java - 序列化错误:如何使用使用 java.util.List[String] 的函数创建使用 ArrayType(StringType) 列的 UDF?