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
And I want to implement it in SQL Server (Version: 2017 and above)
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 |
+-----------------------------------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.