Part 1:
I have data like this:
DECLARE @t TABLE(
MyString NVARCHAR(1000)
);
INSERT INTO @t VALUES
('str1 0319100004919000913 str2 str3 str4 str5 # 42')
,('str1 #127/322410. str2 str3 25.05.2021 str4 12.31.2021 str4.4 str4.1 str6.93; str7: 213583300014958330100100010000000000.')
,('str1 str2 # 7 (str3. #0119200000120014883)')
,('str1 # 156 str2 05.31.2012; 0134200000120005404|NULL')
,('str1 str2 str3 #0321200014120000733 str4')
,('str1 str2 str3 07.07.2021 (RD-GK-320-21-LS-07-12-2021-DTR232) str4.4 str4.1 str6.93') --specific string begins with RD-...
,('str1 str2 str3 30.06.2021 (RD-GK-319-21-LS-12-31-2021-DTR67)str4.4str4.1str6.er67')
,('str1 # IMZ-2021-010906. str2 str3 30.08.2021 str 31.12.2021 str5. str4'); --specific string begins with IMZ-...
The output I expect is
large number:
1. 0319100004919000913
2. 213583300014958330100100010000000000
3. 0119200000120014883
4. 0134200000120005404
5. 0321200014120000733
specific string:
1. RD-GK-320-21-LS-07-12-2021-DTR232
2. RD-GK-319-21-LS-12-31-2021-DTR67
3. IMZ-2021-010906
Part 2:
DECLARE @tbl TABLE(prodID INT, MyString NVARCHAR(1000));
INSERT INTO @tbl VALUES
(1, 'str1 #127 str2 07.02.12 str2 (1- 0387200009121002533, 2- 0387200009121002560)')
,(2, 'str1 # 156 str2 31.05.2012str2; 1) 0121200002121000373; 2) 0134200005921000161')
,(3, 'str1 #1746 str2 05.09.19 str2 (1- str24. str22, 2- 0142200001321000810, 3- 0365100000921001253)|NULL')
,(4, 'str1 #1746 str2 05.09.19 str2 (1,2- str2. str2,str2; 3- 0817200000320013050, 4- 0308200003021000147)')
,(5, 'str1 #1746 str2 от 05.09.19 str2 (1- 0809500000321002170, 2- 0108200000121000197, 3- 0860200000821006486)')
,(6, 'str1 #1817 str2 03.04.20 str2 (1- 0176200005521001714, 2- 0380200000121005479)|NULL')
,(7, 'str1 #1817 str2 03.04.20 str2 (1- 32110289270, 2- 32110848602, 3- 32110882257, 4,5- 32110018979, 6- 32110031431)|NULL');
-- DDL and sample data population, end
for now the output is (if we are using answer of part 1)
The output I expect is
And I want to implement it in SQL Server (Version: 2017 and above)
CodePudding user response:
Please try the following solution.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE(MyString NVARCHAR(1000));
INSERT INTO @tbl VALUES
('str1 0319100004919000913 str2 str3 str4 str5 # 42')
,('str1 #127/322410. str2 str3 25.05.2021 str4 12.31.2021 str4.4 str4.1 str6.93; str7: 213583300014958330100100010000000000.')
,('str1 str2 # 7 (str3. #0119200000120014883)')
,('str1 # 156 str2 05.31.2012; 0134200000120005404|NULL')
,('str1 str2 str3 #0321200014120000733 str4')
,('str1 str2 str3 07.07.2021 (RD-GK-320-21-LS-07-12-2021-DTR232) str4.4 str4.1 str6.93') --specific string begins with RD-...
,('str1 str2 str3 30.06.2021 (RD-GK-319-21-LS-12-31-2021-DTR67)str4.4str4.1str6.er67')
,('str1 # IMZ-2021-010906. str2 str3 30.08.2021 str 31.12.2021 str5. str4'); --specific string begins with IMZ-...
-- DDL and sample data population, end
DECLARE @filter VARCHAR(10) = '#|.(),';
SELECT value
FROM @tbl
CROSS APPLY STRING_SPLIT(TRANSLATE(MyString, @filter, SPACE(LEN(@filter))), SPACE(1))
WHERE TRY_CAST(value AS FLOAT) IS NOT NULL
AND LEN(value) > 9;
SELECT value
FROM @tbl
CROSS APPLY STRING_SPLIT(TRANSLATE(MyString, @filter, SPACE(LEN(@filter))), SPACE(1))
WHERE LEFT(value, 3) IN ('RD-', 'IMZ');
Output #1
--------------------------------------
| value |
--------------------------------------
| 0319100004919000913 |
| 213583300014958330100100010000000000 |
| 0119200000120014883 |
| 0134200000120005404 |
| 0321200014120000733 |
--------------------------------------
Output #2
-----------------------------------
| value |
-----------------------------------
| RD-GK-320-21-LS-07-12-2021-DTR232 |
| RD-GK-319-21-LS-12-31-2021-DTR67 |
| IMZ-2021-010906 |
-----------------------------------


