13位時間戳裝換為日期格式的字符串
select from_unixtime( cast(1597651229344/1000 as int),'yyyy-MM-dd HH:mm:ss')
時間字符串比較大小
Mysql
使用函數:STR_TO_DATE(str, format)
使用示例:select * from h_hotelcontext where now() between STR_TO_DATE(Start_time,'%Y-%m-%d %H:%i:%s') and STR_TO_DATE(End_time,'%Y-%m-%d %H:%i:%s');
使用注意點:format的格式為%Y-%m-%d %H:%i:%s ,中間的分隔符也需要跟你傳入的字符串格式對上
HIVE
沒用過類似的轉化函數,想去找來着,但是突然發現就按比較字符串的邏輯去比較也是可以的
日期類型格式化
Mysql
SELECT DATE_FORMAT(NOW(),'%Y%m%d') -- 2020-08-31 17:46:17 轉化為 20200831
Hive
SELECT DATE_FORMAT(NOW(),'yyyyMMdd') -- 2020-08-31 17:46:17 轉化為 20200831
日期字符串->時間戳
unix_timestamp:日期轉時間戳函數
用法:unix_timestamp(string date)
返回值: bigint
說明: 轉換格式為“yyyy-MM-dd HH:mm:ss“的日期到UNIX時間戳。如果轉化失敗,則返回0。
參考鏈接: https://blog.csdn.net/weixin_37536446/article/details/82419601
select unix_timestamp('2018-09-05 12:01:03') from dw.ceshi_data; 結果如下: 1536120063
時間戳->日期字符串
from_unixtime:時間戳轉日期函數
用法:from_unixtime(bigint unixtime[, stringformat])
返回值: string
說明: 轉化時間戳到當前時區的時間格式
select from_unixtime(1423306743,'yyyyMMdd') from dw.ceshi_data; 結果如下: 20150207
獲取當前日期的時間戳
select unix_timestamp() from dw.ceshi_data; 結果如下: 1536126324