如何检查字符串是否以日期和剥离结果结尾? - SQL

How to check if string ends in date and strip result? - SQL

我正在尝试检查字符串是否以一年结束。

输入:

file_paths
wowefnowinf/wefionwe/wefowoi/2012-02-03
weofnweofn/weoew/2022-03-04
ewpfowe/ewopfew

期望的输出:

wowefnowinf/wefionwe/wefowoi/
weofnweofn/weoew/
ewpfowe/ewopfew

我无法首先检测到字符串本身以日期格式结尾。这是我的查询:

SELECT CASE WHEN 'wowefnowinf/wefionwe/wefowoi/2012-02-03' LIKE '%/####\-##\-##'
 THEN TRUE
 ELSE FALSE END AS result
FROM my_table;

我应该为此得到 true,但我的查询 returns false。任何帮助将不胜感激。

在 Snowflake 中,您可以使用 regexp_likesplit_part:

with dummy as (
    select 'wowefnowinf/wefionwe/wefowoi/2012-02-03' as file_path
    union all
    select 'weofnweofn/weoew/2022-03-04'
    union all
    select 'ewpfowe/ewopfew'
)
select 
    file_path,
    split_part(file_path, '/', -1) as splitted_file_path,
    regexp_like(splitted_file_path, '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]') as ends_with_date,
    iff(ends_with_date, trim(file_path, splitted_file_path), file_path) as trimmed_file_path
from dummy;

输出: