简体   繁体   中英

How to convert date format yyyyMMdd in Hive SQL and automate the query inside the where clause

I would like to get some help in regards of automating my query.

The goal is to get the latest data, everyday the data refreshes but the start date should remain the same.

For example: Start date is Jan 1 2023, but the todays end date should be Jan 13 2023, and next day it will be Jan 14 2023, but the start date stays the same (Jan 1st 2023).

I have tried the following query but it keeps running and do not provide any output. the query I am using is hive SQL and hoping if anyone can help this.

SELECT *  
FROM table_a
WHERE cast(entered_date as date) >= '20230101' 
    and cast(entered_date as date) < date_add(current_date(),0)

You can use date_format(DATE|TIMESTAMP|STRING ts, STRING fmt) to convert value to string in the date format. You can find the description of the function from here . Therefore if you want the output between your desired date and not beyond the current date you can query like this

SELECT *
FROM table
WHERE DATE_FORMAT(entered_date, 'yyyyMMdd') >= '20230101'
  AND DATE_FORMAT(entered_date, 'yyyyMMdd') <= DATE_FORMAT(CURRENT_DATE(), 'yyyyMMdd');

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