ORACLE SQL:删除最后一组括号的最短查询

时间:2017-10-12 03:54:29

标签: oracle

信息:我使用的是Oracle 11g

让我们说这是样本数据:

Lemon(USD4)(RM16)(SG8)
Melon(RM40)(SG20)(USD10)(EU5)
Orange(USD7)(RM28)
Apple(USD2)
Grape

我想替换最后一个括号组(如果有的话)来产生:

Lemon(USD4)(RM16)
Melon(RM40)(SG20)(USD10)
Orange(USD7)
Apple
Grape

这是我的两个SQL似乎太长了:

SELECT
    CASE
        WHEN REGEXP_COUNT(FOOD, '\(' ) = 0 THEN FOOD
        ELSE SUBSTR(FOOD, 1, INSTR(FOOD, '(', -1)-1)
    END
FROM TABLE_A;
SELECT
    DECODE(
        REGEXP_COUNT(FOOD, '\(' ), 0,
        FOOD,
        SUBSTR(FOOD, 1, INSTR(FOOD, '(', -1)-1)
    )
FROM TABLE_A;

我寻求更短的时间。可能是这样的:

SELECT REGEXP_REPLACE(FOOD, '\(what_is_here\)', '', 1, 2) FROM TABLE_A;

1 个答案:

答案 0 :(得分:1)

尝试 [10/12/2017 12:02:00 AM Informational] ------ Discover test started ------ [10/12/2017 12:02:01 AM Informational] NUnit Adapter 3.8.0.0: Test discovery starting [10/12/2017 12:02:01 AM Informational] NUnit Adapter 3.8.0.0: Test discovery complete [10/12/2017 12:02:01 AM Informational] NUnit VS Adapter 2.1.1.0 discovering tests is started [10/12/2017 12:02:01 AM Warning] Dependent Assembly Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a of C:\Users\*****\documents\visual studio 2017\Projects\*****\*****\bin\Debug\*****.dll not found. Can be ignored if not a NUnit project. [10/12/2017 12:02:01 AM Informational] NUnit VS Adapter 2.1.1.0 discovering test is finished [10/12/2017 12:02:01 AM Informational] ========== Discover test finished: 2 found (0:00:00.2788078) ========== [10/12/2017 12:02:01 AM Informational] ------ Run test started ------ [10/12/2017 12:02:01 AM Informational] NUnit Adapter 3.8.0.0: Test execution started [10/12/2017 12:02:01 AM Informational] Running selected tests in C:\Users\******\documents\visual studio 2017\Projects\******\******\bin\Debug\******.dll [10/12/2017 12:02:01 AM Informational] NUnit3TestExecutor converted 1 of 1 NUnit test cases [10/12/2017 12:02:01 AM Warning] TearDown failed for test fixture ******.Specflow.Features.SpecFlowFeature1Feature [10/12/2017 12:02:01 AM Warning] System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. TearDown : System.NullReferenceException : Object reference not set to an instance of an object. [10/12/2017 12:02:01 AM Warning] at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit) at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit) at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.BuildBindingsFromType(Type type) at TechTalk.SpecFlow.Bindings.Discovery.RuntimeBindingRegistryBuilder.BuildBindingsFromAssembly(Assembly assembly) at TechTalk.SpecFlow.TestRunnerManager.BuildBindingRegistry(IEnumerable`1 bindingAssemblies) at TechTalk.SpecFlow.TestRunnerManager.InitializeBindingRegistry(ITestRunner testRunner) at TechTalk.SpecFlow.TestRunnerManager.CreateTestRunner(Int32 threadId) at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(Int32 threadId) at TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(Assembly testAssembly, Nullable`1 managedThreadId) at *****.Specflow.Features.SpecFlowFeature1Feature.FeatureSetup() --TearDown at *****.Specflow.Features.SpecFlowFeature1Feature.FeatureTearDown() [10/12/2017 12:02:01 AM Informational] NUnit Adapter 3.8.0.0: Test execution complete [10/12/2017 12:02:02 AM Informational] ========== Run test finished: 1 run (0:00:00.7393256) ========== 模式:

\([^\)]*\)$

现场演示:http://sqlfiddle.com/#!4/1188b/19

SELECT t.*,
  REGEXP_REPLACE( food, '\([^\)]*\)$', '' ) as xxx
from table1 t