【问题标题】:Extract Substring with variable start and length提取具有可变开始和长度的子字符串
【发布时间】:2020-12-21 15:39:16
【问题描述】:

我有一个来自 horrible 数据库的字段,其中每个值都是一个字符串,例如

现场阶段从“Kortec 销售周期/3 提案”更改为“Kortec 销售周期/推迟”

或者

现场阶段从“Kortec 销售周期/5 次评估”更改为“Kortec 销售周期/6 次实施”

我需要从中创建 2 个计算列,分别命名为“From”和“To”,两个值在引号内或它们以数字开头(除“Postponed”外的所有 7 个阶段都以数字开头)。
这是我最需要的“To”字段。我尝试过使用Instr()InStrRev()LeftMid 函数,但到目前为止还没有任何乐趣。我也在网上搜索过类似的问题,但没有遇到任何这样的文本处理要求。
有谁知道我应该如何解决这个问题?

我(认为我)得到的最接近的是

SELECT RIGHT([OppMovs (Base)].[DETAILS], InStrRev([OppMovs (Base)].[DETAILS], 'to')-3) 
FROM [OppMovs (Base)]

【问题讨论】:

  • 请发布您尝试过的内容,以及结果(如果您接近与否),这让我们可以看到您尝试过的内容,并为我们提供帮助您解决问题的起点。

标签: sql string ms-access text data-manipulation


【解决方案1】:

Split 可用于轻松提取这些:

s = "Field Stage changed from ""Kortec Sales Cycle/3 Proposal"" to ""Kortec Sales Cycle/Postponed""
? s
Field Stage changed from "Kortec Sales Cycle/3 Proposal" to "Kortec Sales Cycle/Postponed"

? Split(Split(s, "from """)(1), """")(0)
Kortec Sales Cycle/3 Proposal

? Split(Split(s, "to """)(1), """")(0)
Kortec Sales Cycle/Postponed

但是,要在查询中使用,您必须创建小的帮助函数,例如:

Public Function GetFrom(ByVal Value As String) As String

    GetFrom = Split(Split(Value, "from """)(1), """")(0)

End Function


Public Function GetTo(ByVal Value As String) As String

    GetTo = Split(Split(Value, "to """)(1), """")(0)

End Function

【讨论】:

    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多