hive date function finishing

Source: http://blog.csdn.net/zhlelva/article/details/54924507
Comments:

1. Date function

1) Timestamp: unix_timestamp/from_unixtime

select unix_timestamp(); -- query the timestamp of the current time and return 1486524284
select unix_timestamp('2017-02-08 11:15:50'); --Query the timestamp of the specified time and return 1486523750 (if the conversion fails, return 0)
select unix_timestamp('20170208 11:00:00','yyyyMMdd HH: mm:ss'); --Convert the timestamp of the specified format time
select from_unixtime(1486523750); --Query the time of the specified timestamp, the default format is yyyy-MM-dd HH:mm:ss , return 2017-02-08 11 :15:50
select from_unixtime(1486523750,'yyyyMMdd'); --Query the time of the specified timestamp, convert it into the specified format, and return 20170208

2) Date extraction year, month, day, hour, minute, second + week: year/month/day/hour/ minute/second/weekofyear

select year('2017-02-08 11:15:50'),month('2017-02-08 11:15:50'),day('2017-02-08 11:15:50'),hour ('2017-02-08 11:15:50'),minute('2017-02-08 11:15:50'),second('2017-02-08 11:15:50'),weekofyear(' 2017-02-08 11:15:50'); --Query the year, month, day, hour, minute, second and week of the specified date, and return 2017, 2, 8, 11, 15, 50, 6
3) Date time to date: to_date

select to_date('2017-02-08 11:15:50'); --Query the date in the specified date time. Return 2017-02-08

4) Date comparison

-- date comparison, the number of days between the end time and the start time. datediff(string enddate, string startdate)

select datediff('2017-02-02','2017-01-31');--return 2

--date increase and decrease. date_sub (string startdate, int days)

select date_add('2017-01-31',2); --return 2017-02-02

select date_sub('2017-02-02',2);







select get_date(-2);--Return the date offset by 2 days from the current date, return 2017-02-07

--Custom date operation function (return date without horizontal line): get_dt_date

select get_dt_date(); --Get the current date and return 20170209

select get_dt_date(get_date(-2));--Get the current date offset and convert it to a format without a bar

select get_dt_date('2017-02-02',-2);- -20170131

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228041&siteId=291194637