简体   繁体   中英

selecting date with regexp_extract

I will need to extract the year from a column name, , it is returning null value and the same number of character. i would want to only extract the date however there is a few column with the same number of character.

sample data in table

10020020
1172053041
597246141

3339110821
26590621
192133643
20190203
20180109
20170204
20190904

I have tried this,

select regexp_extract((colname), '([0-9]{8})', 1) from tablename

however it is returning the result that has the same number of characters with null values. i wish to only extract only the date which is 20170109,20190204 etc etc. what is the best approach and what did i go wrong?

    10020020
    
    26590621
    20190203
    20180109
    20170204
    20190904

i have tried using wildcard select regexp_extract((maxvalue), '([0-9]{8})', 1) like '%2019%' from profilingoverviewreport but it returning boolean instead

If you want to match values which have exactly 8 digits, and those 8 digit values correspond to dates, then I suggest the following pattern:

^(20|19)[0-9]{6}$

Your updated SQL code:

SELECT *
FROM tablename
WHERE colname IREGEXP '^(20|19)[0-9]{6}$';

Check the demo to see the regex pattern correctly identifying the dates in your column:

Demo

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM