I have a case where I have to extract date from a SQL query that I am running through a python script.
This is the query:
select * from COLL_DS2 where TRANSACTION_DATE between '2021.02.28' and '2021.04.08' order by CREATION_DATE
I want to extract '2021.02.28' and '2021.04.08' from the query string.
Any help please? Thanks in advance
You may use re.findall
:
sql = "select * from COLL_DS2 where TRANSACTION_DATE between '2021.02.28' and '2021.04.08' order by CREATION_DATE"
dates = re.findall(r"'(\d{4}\.\d{2}\.\d{2})'", sql)
print(dates) # ['2021.02.28', '2021.04.08']
Another way to write it. Tim Biegeleisen solution is good, just need to add the import re
import re
Query = "select * from COLL_DS2 where TRANSACTION_DATE between '2021.02.28' and '2021.04.08'"
ldat = re.findall('\d+\.\d+\.\d+', Query)
print(ldat)
# ['2021.02.28', '2021.04.08']
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.