網站首頁 編程語言 正文
獲取當天0點的時間:
select from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00');
執行結果為:
2022-07-08 00:00:00 |
獲取當天0點的秒級時間戳:
select unix_timestamp(from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00'));
執行結果:
1657209600 |
將時間作為條件,條件查詢
查詢創建時間(格式為yyyy-MM-dd HH:mm:ss)在前一天0點到今天0點之間的數據
select *?from table_t where created_time >?from_unixtime(unix_timestamp()-86400,'yyyy-MM-dd 00:00:00') and created_time <=?from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00')
若條件字段為時間戳使用時間戳,則同理使用unix_timestamp(from_unixtime(unix_timestamp(),'yyyy-MM-dd 00:00:00'))。
以上。
PS:
補充hive時間日期函數,備忘
以下轉自hive時間日期函數總結 - 簡書
-- 1、hive取得當前日期時間:
-- 1.1) 取得當前日期:
select current_date();
-- 1.2) 取得當前日期時間:
select current_timestamp();
-- 1.3) hive取得當前時間戳:
select unix_timestamp();
-- 1.4) 時間戳轉日期:
select from_unixtime(1517725479,'yyyy-MM-dd HH:dd:ss');
-- 1.5) 日期轉unix時間戳:
select to_nuix_timestamp('2017-01-01 12:12:12','yyyy-MM-dd HH:dd:ss');
-- 1.6) hive取得當前時間:
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');
-- 2、hive自動計算其他日期(昨天,今天):
-- hive中日期加減函數:date_add(start_date,num_days)
-- 2.1) 取得昨天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_format(date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
-- 2.2) 取得明天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
-- 2.3)hive取得兩個日期之間差值(差值為天數):
-- datediff(date1,date2):date1大于date2,返回值為正,否則,返回值為負。
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10));
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));
-- 2.4) 字符串轉時間(字符串必須為:yyyy-MM-dd格式)
select to_date('2017-01-01 12:12:12');
-- 2.5) 日期、時間戳、字符串類型格式化輸出標準時間格式:
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
select date_format(current_date(),'yyyyMMdd');
select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss'); --字符串必須滿足yyyy-MM-dd格式
-- 2.6) utc時間轉換:
select from_utc_timestamp(current_timestamp(),8);
select to_utc_timestamp(current_timestamp(),8);
原文鏈接:https://blog.csdn.net/jian876601394/article/details/125681613
相關推薦
- 2023-04-08 C#字符串和Acsii碼相互轉換_C#教程
- 2023-07-09 qsort(),sort()排序函數
- 2022-06-20 一文帶你掌握Go語言運算符的使用_Golang
- 2022-09-06 Go?語言選擇器實例教程_Golang
- 2022-10-09 Python?pygame項目實戰監聽退出事件_python
- 2023-06-04 Kotlin比較與解釋Lazy與Lateinit的用法_Android
- 2022-11-10 詳解C++?左值引用與?const?關鍵字_C 語言
- 2023-02-07 C++實現字符串和整數的相互轉換_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支