SQL Server SQL query to pick the correct substring value from the column value

b4lqfgs4  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(108)

I have the following table with the column Brand need to extract the correct string as in the output table.

Source Table

Brand
(NORLANE) MADDERN 1200 (CONTEMPORARY 1 FACADE)
do not useASCENT 2600 ASPEN
do not useEVOLUTION 2300 ONTARIO
do not useMERCHISTON 3800 TRADITIONAL
AERO 1600 BLAKE
ALBANY 1700 ASPEN (GENESIS)
Albion 161 - Matisse - Discontinued
Alexander 159 - Coastal
Almeda 344 - Huntington
Mendelsohn 234 Swift - Drysdale
Andre Swift 235 - Condor - In progress
HARLECH 333
Seymour 284 - Drysdale - Master
OLSEN DUPLEX 2100 COMTEMPORARY (LOT 1 & 2)

Output required

Brand
MADDERN 1200
ASCENT 2600
EVOLUTION 2300
MERCHISTON 3800
AERO 1600
ALBANY 1700
Albion 161
Alexander 159
Almeda 344
Mendelsohn 234
Andre Swift 235
HARLECH 333
Seymour 284
OLSEN DUPLEX 2100
Script 
------------
SELECT *

INTO  #tmpTest
FROM
(
SELECT '(NORLANE) MADDERN 1200 (CONTEMPORARY 1 FACADE)' Brand
UNION ALL
SELECT '**do not use**ASCENT 2600 ASPEN'
UNION ALL
SELECT '**do not use**EVOLUTION 2300 ONTARIO'
UNION ALL
SELECT '**do not use**MERCHISTON 3800 TRADITIONAL'
UNION ALL
SELECT 'AERO 1600 BLAKE'
UNION ALL
SELECT 'ALBANY 1700 ASPEN (GENESIS)'
UNION ALL
SELECT 'Albion 161 - Matisse - Discontinued'
UNION ALL
SELECT 'Alexander 159 - Coastal'
UNION ALL
SELECT 'Almeda 344 - Huntington'
UNION ALL
SELECT 'Mendelsohn 234 Swift - Drysdale'
UNION ALL
SELECT 'Andre Swift 235 - Condor - In progress'
UNION ALL
SELECT 'HARLECH 333'
UNION ALL
SELECT 'Seymour  284  - Drysdale - Master'
UNION ALL
SELECT 'OLSEN DUPLEX 2100 COMTEMPORARY (LOT 1 & 2)'
)R

Used the following query, there is an issue with the query which is not returning the proper output.

SELECT Brand,LEFT(s2.s, p2.p) as CorrectBrand
FROM #tmpTest
CROSS APPLY(SELECT REPLACE(Brand, '**do not use**',''))s1(s) /* Source string with text removed */
CROSS APPLY(SELECT CHARINDEX(' ', s1.s))p1(FirstSpace) /* Find the pos of first space */
CROSS APPLY(SELECT CHARINDEX(')', s1.s))b(ClosePar) /* Find pos of first close parenthesis */
CROSS APPLY(SELECT IIF(ClosePar < FirstSpace and ClosePar > 0, STUFF(s1.s, 1, ClosePar + 1, ''), s1.s))s2(s) /* Remove content up to first closing parenthesisif relevant */
CROSS APPLY(SELECT CHARINDEX(' ', s2.s, FirstSpace + 1))p2(p); /* Pos of second space */
a11xaf1n

a11xaf1n1#

Approaches like this almost always lead to discovery of new exceptions, the better way to solve this is to store the correct strings. (Perhaps that's what you hope to do?)

Anyway, here is a similar approach but which meets the current expected result:

SELECT
    Brand
  , ca3.CorrectBrand
FROM #tmpTest
CROSS APPLY (SELECT REPLACE(Brand, '**do not use**','')
            ) ca1(s)
CROSS APPLY (select substring(ca1.s,1,   
               PATINDEX('%[0-9]%', ca1.s) + CHARINDEX(' ',(substring(ca1.s,PATINDEX('%[0-9]%', ca1.s),100) + ' '))-2
               ) /* substring up to end of digits */
            ) ca2(s)  
CROSS APPLY (SELECT trim(substring(ca2.s, coalesce(CHARINDEX(')', ca2.s)+1,1),100))
            ) ca3(CorrectBrand) /* remove text within (...) */
BrandCorrectBrand
(NORLANE) MADDERN 1200 (CONTEMPORARY 1 FACADE)MADDERN 1200
do not useASCENT 2600 ASPENASCENT 2600
do not useEVOLUTION 2300 ONTARIOEVOLUTION 2300
do not useMERCHISTON 3800 TRADITIONALMERCHISTON 3800
AERO 1600 BLAKEAERO 1600
ALBANY 1700 ASPEN (GENESIS)ALBANY 1700
Albion 161 - Matisse - DiscontinuedAlbion 161
Alexander 159 - CoastalAlexander 159
Almeda 344 - HuntingtonAlmeda 344
Mendelsohn 234 Swift - DrysdaleMendelsohn 234
Andre Swift 235 - Condor - In progressAndre Swift 235
HARLECH 333HARLECH 333
Seymour 284 - Drysdale - MasterSeymour 284
OLSEN DUPLEX 2100 COMTEMPORARY (LOT 1 & 2)OLSEN DUPLEX 2100

fiddle

相关问题