create table #randomTexts (col1 nvarchar(max));
insert into #randomTexts values ('randomtext20221108randomtext');
insert into #randomTexts values ('2022asdasd20211108randomtext');
insert into #randomTexts values ('1234567820181108randomtext');
insert into #randomTexts values ('xxx20331332zzz20221108randomtext');
WITH CTE_Numbers AS
(
SELECT MAX(LEN(col1))-1 as n FROM #randomTexts
UNION ALL
SELECT n-1 FROM CTE_Numbers
WHERE n>0
)
SELECT col1,CAST(SUBSTRING(col1,n,8) AS DATE) AS validDate, SUBSTRING(col1,n,4) AS year
FROM #randomTexts
CROSS JOIN CTE_Numbers
WHERE TRY_CAST(SUBSTRING(col1,n,8) AS DATE) IS NOT NULL
AND n+8<= LEN(col1);
3条答案
按热度按时间nxagd54h1#
我想这个查询可能会对您有所帮助,
如果你只需要一年,你可以
polhcujo2#
下面的例子可能对您有用,假设日期都是本世纪
knsnq2tg3#