【问题标题】:How to get a substring between certain characters in SQL Server如何在 SQL Server 中的某些字符之间获取子字符串
【发布时间】:2016-09-26 22:02:43
【问题描述】:

我有一个字符串com.test.edu.personal.SomeException: this is some error: test message。我需要将字符串作为SomeException: this is some error

我的代码有问题:

declare @col varchar(100)

set @col = 'com.test.edu.personal.SomeException: this is some error: test message'

SELECT 
    SUBSTRING(@col, 
              LEN(@col) - CHARINDEX(':', @col) - CHARINDEX(':', REVERSE(@col)),
              LEN(@col) - LEN(LEFT(@col, CHARINDEX ('.', @col))) - LEN(RIGHT(@col, LEN(@col) - CHARINDEX (':', @col))));

我收到nal.SomeException: this is some - 我在这里遗漏了一些东西

【问题讨论】:

  • 定义该特定子字符串的规则是什么?

标签: sql sql-server substring


【解决方案1】:
declare @col varchar(100)
set @col = 'com.test.edu.personal.SomeException: this is some error: test message'

Select Substring(@col,Len((substring(@col,1,charindex(':',@col)))) - CharIndex(Reverse('.'), Reverse((substring(@col,1,charindex(':',@col)))))-len('.')+3 ,len(@col))

返回

SomeException: this is some error: test message

【讨论】:

  • 谢谢,我也需要 SomeException:。我需要提取:“SomeException:这是一些错误”
  • 我想我问得不对。字符串 SomeException 是可变的,我可以获得不同的异常。我需要从第一个返回:直到最后。
  • @Lokesh 不用担心,只需使用更可靠的样本和所需结果更新您的问题。这样我们就可以避免猜谜游戏了。
  • @Lokesh 我明白你在找什么……等一下
  • @Lokesh 更新
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 2016-12-08
  • 2013-04-07
  • 2020-06-25
  • 1970-01-01
  • 2012-01-14
  • 2010-12-23
相关资源
最近更新 更多