- 最新动态
- 功能总览
- 服务公告
- 产品介绍
- 计费说明
- 快速入门
-
用户指南
- DLI作业开发流程
- 准备工作
- 创建弹性资源池和队列
- 创建数据库和表
- 数据迁移与数据传输
- 配置DLI访问其他云服务的委托权限
- 使用DLI提交SQL作业
- 使用DLI提交Flink作业
- 使用DLI提交Spark作业
- 使用Notebook实例提交DLI作业
- 使用CES监控DLI服务
- 使用CTS审计DLI服务
- 权限管理
- DLI常用管理操作
- 最佳实践
-
开发指南
- 使用客户端工具连接DLI
- SQL作业开发指南
- Flink作业开发指南
- Spark Jar作业开发指南
-
Spark SQL语法参考
- Spark SQL常用配置项说明
- Spark SQL语法概览
- Spark开源命令支持说明
- 数据库相关
- 表相关
- 数据相关
- 导出查询结果
- 跨源连接相关
- 视图相关
- 查看计划
- 数据权限相关
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- class_name
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- hdfs_path
- input_expression
- input_format_classname
- jar_path
- join_condition
- non_equi_join_condition
- number
- num_buckets
- output_format_classname
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- row_format
- select_statement
- separator
- serde_name
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- view_name
- view_properties
- when_expression
- where_condition
- window_function
- 运算符
-
Flink SQL语法参考
- Flink Opensource SQL1.15语法参考
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
-
HetuEngine SQL语法参考
-
HetuEngine SQL语法
- 使用前必读
- 数据类型
-
DDL 语法
- CREATE SCHEMA
- CREATE TABLE
- CREATE TABLE AS
- CREATE TABLE LIKE
- CREATE VIEW
- ALTER TABLE
- ALTER VIEW
- ALTER SCHEMA
- DROP SCHEMA
- DROP TABLE
- DROP VIEW
- TRUNCATE TABLE
- COMMENT
- VALUES
- SHOW语法使用概要
- SHOW SCHEMAS(DATABASES)
- SHOW TABLES
- SHOW TBLPROPERTIES TABLE|VIEW
- SHOW TABLE/PARTITION EXTENDED
- SHOW FUNCTIONS
- SHOW PARTITIONS
- SHOW COLUMNS
- SHOW CREATE TABLE
- SHOW VIEWS
- SHOW CREATE VIEW
- DML 语法
- DQL 语法
- 辅助命令语法
- 预留关键字
- SQL函数和操作符
- 数据类型隐式转换
- 附录
-
HetuEngine SQL语法
- Hudi SQL语法参考
- Delta SQL语法参考
-
API参考
- API使用前必读
- API概览
- 如何调用API
- API快速入门
- 权限相关API
- 全局变量相关API
- 资源标签相关API
- 增强型跨源连接相关API
- 跨源认证相关API
- 弹性资源池相关API
- 队列相关API(推荐)
- SQL作业相关API
- SQL模板相关API
- Flink作业相关API
- Flink作业模板相关API
- Flink作业管理相关API
- Spark作业相关API
- Spark作业模板相关API
- 权限策略和授权项
- 历史API
- 公共参数
- SDK参考
-
常见问题
- DLI产品咨询类
- DLI弹性资源池和队列类
-
DLI数据库和表类
- 为什么在DLI控制台中查询不到表?
- OBS表压缩率较高怎么办?
- 字符码不一致导致数据乱码怎么办?
- 删除表后再重新创建同名的表,需要对操作该表的用户和项目重新赋权吗?
- DLI分区内表导入的文件不包含分区列的数据,导致数据导入完成后查询表数据失败怎么办?
- 创建OBS外表,由于OBS文件中的某字段存在换行符导致表字段数据错误怎么办?
- join表时没有添加on条件,造成笛卡尔积查询,导致队列资源爆满,作业运行失败怎么办?
- 手动在OBS表的分区目录下添加了数据,但是无法查询到数据怎么办?
- 为什么insert overwrite覆盖分区表数据的时候,覆盖了全量数据?
- 跨源连接RDS表中create_date字段类型是datetime,为什么DLI中查出来的是时间戳呢?
- SQL作业执行完成后,修改表名导致datasize不正确怎么办?
- 从DLI导入数据到OBS,数据量不一致怎么办?
-
增强型跨源连接类
- 增强型跨源连接绑定队列失败怎么办?
- DLI增强型跨源连接DWS失败怎么办?
- 创建跨源成功但测试网络连通性失败怎么办?
- 怎样配置DLI队列与数据源的网络连通?
- 为什么DLI增强型跨源连接要创建对等连接?
- DLI创建跨源连接,绑定队列一直在创建中怎么办?
- 新建跨源连接,显示已激活,但使用时提示communication link failure错误怎么办?
- 跨源访问MRS HBase,连接超时,日志未打印错误怎么办?
- DLI跨源连接报错找不到子网怎么办?
- 跨源RDS表,执行insert overwrite提示Incorrect string value错误怎么办?
- 创建RDS跨源表提示空指针错误怎么办?
- 对跨源DWS表执行insert overwrite操作,报错:org.postgresql.util.PSQLException: ERROR: tuple concurrently updated
- 通过跨源表向CloudTable Hbase表导入数据,executor报错:RegionTooBusyException
- 通过DLI跨源写DWS表,非空字段出现空值异常怎么办?
- 更新跨源目的端源表后,未同时更新对应跨源表,导致insert作业失败怎么办?
- RDS表有自增主键时怎样在DLI插入数据?
-
SQL作业类
- SQL作业开发类
-
SQL作业运维类
- 用户导表到OBS报“path obs://xxx already exists”错误
- 对两个表进行join操作时,提示:SQL_ANALYSIS_ERROR: Reference 't.id' is ambiguous, could be: t.id, t.id.;
- 执行查询语句报错:The current account does not have permission to perform this operation,the current account was restricted. Restricted for no budget.
- 执行查询语句报错:There should be at least one partition pruning predicate on partitioned table XX.YYY
- LOAD数据到OBS外表报错:IllegalArgumentException: Buffer size too small. size
- SQL作业运行报错:DLI.0002 FileNotFoundException
- 用户通过CTAS创建hive表报schema解析异常错误
- 在DataArts Studio上运行DLI SQL脚本,执行结果报org.apache.hadoop.fs.obs.OBSIOException错误
- 使用CDM迁移数据到DLI,迁移作业日志上报UQUERY_CONNECTOR_0001:Invoke DLI service api failed错误
- SQL作业访问报错:File not Found
- SQL作业访问报错:DLI.0003: AccessControlException XXX
- SQL作业访问外表报错:DLI.0001: org.apache.hadoop.security.AccessControlException: verifyBucketExists on {{桶名}}: status [403]
- 执行SQL语句报错:The current account does not have permission to perform this operation,the current account was restricted. Restricted for no budget.
-
Flink作业类
- Flink作业咨询类
-
Flink SQL作业类
- 怎样将OBS表映射为DLI的分区表?
- Flink SQL作业Kafka分区数增加或减少,怎样不停止Flink作业实现动态感知?
- 在Flink SQL作业中创建表使用EL表达式,作业运行提示DLI.0005错误怎么办?
- Flink作业输出流写入数据到OBS,通过该OBS文件路径创建的DLI表查询无数据
- Flink SQL作业运行失败,日志中有connect to DIS failed java.lang.IllegalArgumentException: Access key cannot be null错误
- Flink SQL作业消费Kafka后sink到es集群,作业执行成功,但未写入数据
- Flink Opensource SQL如何解析复杂嵌套 JSON?
- Flink Opensource SQL从RDS数据库读取的时间和RDS数据库存储的时间为什么会不一致?
- Flink Opensource SQL Elasticsearch结果表failure-handler参数填写retry_rejected导致提交失败
- Kafka Sink配置发送失败重试机制
- 如何在一个Flink作业中将数据写入到不同的Elasticsearch集群中?
- 作业语义检验时提示DIS通道不存在怎么处理?
- Flink jobmanager日志一直报Timeout expired while fetching topic metadata怎么办?
- Flink Jar作业类
- Flink作业性能调优类
-
Spark作业相类
- Spark作业开发类
-
Spark作业运维类
- 运行Spark作业报java.lang.AbstractMethodError
- Spark作业访问OBS数据时报ResponseCode: 403和ResponseStatus: Forbidden错误
- 有访问OBS对应的桶的权限,但是Spark作业访问时报错 verifyBucketExists on XXXX: status [403]
- Spark作业运行大批量数据时上报作业运行超时异常错误
- 使用Spark作业访问sftp中的文件,作业运行失败,日志显示访问目录异常
- 执行作业的用户数据库和表权限不足导致作业运行失败
- 为什么Spark3.x的作业日志中打印找不到global_temp数据库
- 在使用Spark2.3.x访问元数据时,DataSource语法创建avro类型的OBS表创建失败
- DLI资源配额类
- DLI权限管理类
- DLI API类
- 视频帮助
-
更多文档
- 用户指南(阿布扎比区域)
- API参考(阿布扎比区域)
-
SQL语法参考(阿布扎比区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 用户指南(巴黎区域)
- API参考 (巴黎区域)
-
SQL语法参考(巴黎区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 用户指南(吉隆坡区域)
- API参考(吉隆坡区域)
-
SQL语法参考(吉隆坡区域)
-
Spark SQL语法参考
- 批作业SQL常用配置项说明
- 批作业SQL语法概览
- Spark开源命令支持说明
- 数据库
- 创建OBS表
- 创建DLI表
- 删除表
- 查看表
- 修改表
- 分区表相关
- 导入数据
- 插入数据
- 清空数据
- 导出查询结果
- 多版本备份恢复数据
- 表生命周期管理
- 跨源连接HBase表
- 跨源连接OpenTSDB表
- 跨源连接DWS表
- 跨源连接RDS表
- 跨源连接CSS表
- 跨源连接DCS表
- 跨源连接DDS表
- 跨源连接Oracle表
- 视图
- 查看计划
- 数据权限管理
- 数据类型
- 自定义函数
-
内置函数
-
日期函数
- 日期函数概览
- add_months
- current_date
- current_timestamp
- date_add
- dateadd
- date_sub
- date_format
- datediff
- datediff1
- datepart
- datetrunc
- day/dayofmonth
- from_unixtime
- from_utc_timestamp
- getdate
- hour
- isdate
- last_day
- lastday
- minute
- month
- months_between
- next_day
- quarter
- second
- to_char
- to_date
- to_date1
- to_utc_timestamp
- trunc
- unix_timestamp
- weekday
- weekofyear
- year
-
字符串函数
- 字符串函数概览
- ascii
- concat
- concat_ws
- char_matchcount
- encode
- find_in_set
- get_json_object
- instr
- instr1
- initcap
- keyvalue
- length
- lengthb
- levenshtein
- locate
- lower/lcase
- lpad
- ltrim
- parse_url
- printf
- regexp_count
- regexp_extract
- replace
- regexp_replace
- regexp_replace1
- regexp_instr
- regexp_substr
- repeat
- reverse
- rpad
- rtrim
- soundex
- space
- substr/substring
- substring_index
- split_part
- translate
- trim
- upper/ucase
- 数学函数
- 聚合函数
- 分析窗口函数
- 其他函数
-
日期函数
- SELECT基本语句
- 过滤SELECT
- 排序SELECT
- 分组SELECT
- 连接操作SELECT
- 子查询
- 别名SELECT
- 集合运算SELECT
- WITH...AS
- CASE...WHEN
- OVER子句
- Flink Opensource SQL1.12语法参考
- Flink Opensource SQL1.10语法参考
- 历史版本
-
标示符
- aggregate_func
- alias
- attr_expr
- attr_expr_list
- attrs_value_set_expr
- boolean_expression
- col
- col_comment
- col_name
- col_name_list
- condition
- condition_list
- cte_name
- data_type
- db_comment
- db_name
- else_result_expression
- file_format
- file_path
- function_name
- groupby_expression
- having_condition
- input_expression
- join_condition
- non_equi_join_condition
- number
- partition_col_name
- partition_col_value
- partition_specs
- property_name
- property_value
- regex_expression
- result_expression
- select_statement
- separator
- sql_containing_cte_name
- sub_query
- table_comment
- table_name
- table_properties
- table_reference
- when_expression
- where_condition
- window_function
- 运算符
-
Spark SQL语法参考
- 通用参考
链接复制成功!
日期、时间函数及运算符
日期时间运算符
运算符 |
示例 |
结果 |
---|---|---|
+ |
date '2012-08-08' + interval '2' day |
2012-08-10 |
+ |
time '01:00' + interval '3' hour |
04:00:00.000 |
+ |
timestamp '2012-08-08 01:00' + interval '29' hour |
2012-08-09 06:00:00.000 |
+ |
timestamp '2012-10-31 01:00' + interval '1' month |
2012-11-30 01:00:00.000 |
+ |
interval '2' day + interval '3' hour |
2 03:00:00.000 |
+ |
interval '3' year + interval '5' month |
3-5 |
- |
date '2012-08-08' - interval '2' day |
2012-08-06 |
- |
time '01:00' - interval '3' hour |
22:00:00.000 |
- |
timestamp '2012-08-08 01:00' - interval '29' hour |
2012-08-06 20:00:00.000 |
- |
timestamp '2012-10-31 01:00' - interval '1' month |
2012-09-30 01:00:00.000 |
- |
interval '2' day - interval '3' hour |
1 21:00:00.000 |
- |
interval '3' year - interval '5' month |
2-7 |
时区转换
运算符:AT TIME ZONE,用于设置一个时间戳的时区。
SELECT timestamp '2012-10-31 01:00 UTC';-- 2012-10-31 01:00:00.000 UTC SELECT timestamp '2012-10-31 01:00 UTC' AT TIME ZONE 'Asia/Singapore'; -- 2012-10-30 09:00:00.000 Asia/Singapore
- current_time -> time with time zone
select current_time;-- 16:58:48.601+08:00
- current_timestamp -> timestamp with time zone
select current_timestamp; -- 2020-07-25 11:50:27.350 Asia/Singapore
- current_timezone() → varchar
select current_timezone();-- Asia/Singapore
- date(x) → date
select date('2020-07-25');-- 2020-07-25
- from_iso8601_timestamp(string) → timestamp with time zone
SELECT from_iso8601_timestamp('2020-05-11');-- 2020-05-11 00:00:00.000 Asia/Singapore SELECT from_iso8601_timestamp('2020-05-11T11:15:05'); -- 2020-05-11 11:15:05.000 Asia/Singapore SELECT from_iso8601_timestamp('2020-05-11T11:15:05.055+01:00');-- 2020-05-11 11:15:05.055 +01:00
- from_iso8601_date(string) → date
SELECT from_iso8601_date('2020-05-11');-- 2020-05-11 SELECT from_iso8601_date('2020-W10');-- 2020-03-02 SELECT from_iso8601_date('2020-123');-- 2020-05-02
- from_unixtime(unixtime) → timestamp with time zone
Select FROM_UNIXTIME(1.595658735E9); -- 2020-07-25 14:32:15.000 Asia/Singapore Select FROM_UNIXTIME(875996580); --1997-10-05 04:23:00.000 Asia/Singapore
- from_unixtime(unixtime, string) → timestamp with time zone
select from_unixtime(1.595658735E9, 'Asia/Singapore');-- 2020-07-25 14:32:15.000 Asia/Singapore
- from_unixtime(unixtime, hours, minutes) → timestamp with time zone
将UNIX时戳转换成带时区的时戳变量,hours和minutes表示时区偏移量
select from_unixtime(1.595658735E9, 8, 30);-- 2020-07-25 14:32:15.000 +08:30
- localtime -> time
select localtime;-- 14:16:13.096
- localtimestamp -> timestamp
select localtimestamp;-- 2020-07-25 14:17:00.567
- months_between(date1, date2) -> double
返回date1和date2之间的月数,如果date1比date2迟,结果就是正数,那么结果就是负数;如果两个日期的日数相同,那么结果就是整数,否则按照每月31天以及时分秒的差异来计算小数部分。date1和date2的类型可以是date,timestamp,也可以是“yyyy-MM-dd”或“yyyy-MM-dd HH:mm:ss”格式的字符串
select months_between('2020-02-28 10:30:00', '2021-10-30');-- -20.05040323 select months_between('2021-01-30', '2020-10-30'); -- 3.0
- now() → timestamp with time zone
select now();-- 2020-07-25 14:39:39.842 Asia/Singapore
- unix_timestamp()
select unix_timestamp(); -- 1600930503
- to_iso8601(x) → varchar
将x转换成ISO8601格式的字符串。这里x可以是DATE、TIMESTAMP [with time zone]这几个类型
select to_iso8601(date '2020-07-25'); -- 2020-07-25 select to_iso8601(timestamp '2020-07-25 15:22:15.214'); -- 2020-07-25T15:22:15.214
- to_milliseconds(interval) → bigint
select to_milliseconds(interval '8' day to second);-- 691200000
- to_unixtime(timestamp) → double
select to_unixtime(cast('2020-07-25 14:32:15.147' as timestamp));-- 1.595658735147E9
- trunc(string date, string format) →string
按照format格式去截取日期值,支持的格式有:MONTH/MON/MM,YEAR/YYYY/YY, QUARTER/Q
select trunc(date '2020-07-08','yy');-- 2020-01-01 select trunc(date '2020-07-08','MM');-- 2020-07-01
说明:
使用下列 SQL 标准函数时,兼容使用圆括号的方式:
- current_date
- current_time
- current_timestamp
- localtime
- Localtimestamp
如:select current_date();
截取函数
类似于保留几位小数的操作,函数 date_trunc 支持如下单位:
单位 |
截取后的值 |
---|---|
second |
2001-08-22 03:04:05.000 |
minute |
2001-08-22 03:04:00.000 |
hour |
2001-08-22 03:00:00.000 |
day |
2001-08-22 00:00:00.000 |
week |
2001-08-20 00:00:00.000 |
month |
2001-08-01 00:00:00.000 |
quarter |
2001-07-01 00:00:00.000 |
year |
2001-01-01 00:00:00.000 |
上面的例子使用时间戳2001-08-22 03:04:05.321作为输入。
date_trunc(unit, x) → [same as input]
返回x截取到单位unit之后的值。
select date_trunc('hour', timestamp '2001-08-22 03:04:05.321'); -- 2001-08-22 03:00:00.000
间隔函数
本章中的函数支持如下所列的间隔单位:
单位 |
描述 |
---|---|
second |
Seconds |
minute |
Minutes |
hour |
Hours |
day |
Days |
week |
Weeks |
month |
Months |
quarter |
Quarters of a year |
year |
Years |
- date_add(unit, value, timestamp) → [same as input]
在timestamp的基础上加上value个unit。如果想要执行相减的操作,可以通过将value赋值为负数来完成。
SELECT date_add('second', 86, TIMESTAMP '2020-03-01 00:00:00');-- 2020-03-01 00:01:26 SELECT date_add('hour', 9, TIMESTAMP '2020-03-01 00:00:00');-- 2020-03-01 09:00:00 SELECT date_add('day', -1, TIMESTAMP '2020-03-01 00:00:00 UTC');-- 2020-02-29 00:00:00 UTC
- date_diff(unit, timestamp1, timestamp2) → bigint
返回timestamp2 - timestamp1之后的值,该值的表示单位是unit。
unit的值是字符串。例如:‘day’、‘week’、‘year’
SELECT date_diff('second', TIMESTAMP '2020-03-01 00:00:00', TIMESTAMP '2020-03-02 00:00:00');-- 86400 SELECT date_diff('hour', TIMESTAMP '2020-03-01 00:00:00 UTC', TIMESTAMP '2020-03-02 00:00:00 UTC');-- 24 SELECT date_diff('day', DATE '2020-03-01', DATE '2020-03-02');-- 1 SELECT date_diff('second', TIMESTAMP '2020-06-01 12:30:45.000', TIMESTAMP '2020-06-02 12:30:45.123');-- 86400 SELECT date_diff('millisecond', TIMESTAMP '2020-06-01 12:30:45.000', TIMESTAMP '2020-06-02 12:30:45.123');-- 86400123
- adddate(date, bigint)→ [same as input]
持续时间函数
持续时间可以使用以下单位:
单位 |
描述 |
---|---|
ns |
纳秒 |
us |
微秒 |
ms |
毫秒 |
s |
秒 |
m |
分钟 |
h |
小时 |
d |
天 |
parse_duration(string) → interval
SELECT parse_duration('42.8ms'); -- 0 00:00:00.043 SELECT parse_duration('3.81 d'); -- 3 19:26:24.000 SELECT parse_duration('5m'); -- 0 00:05:00.000
MySQL 日期函数
在这一章节使用与MySQL date_parse和str_to_date方法兼容的格式化字符串。
- date_format(timestamp, format) → varchar
select date_format(timestamp '2020-07-22 15:00:15', '%Y/%m/%d');-- 2020/07/22
- date_parse(string, format) → timestamp
select date_parse('2020/07/20', '%Y/%m/%d');-- 2020-07-20 00:00:00.000
下面的表格是基于MySQL手册列出的,描述了各种格式化描述符:
格式化描述符 |
描述 |
---|---|
%a |
对应的星期几(Sun .. Sat) |
%b |
对应的月份(Jan .. Dec) |
%c |
对应的月份(1 .. 12) |
%D |
对应该月的第几天(0th, 1st, 2nd, 3rd, ...) |
%d |
对应该月的第几天,数字(01 .. 31)(两位,前面会补0) |
%e |
对应该月的第几天,数字(1 .. 31) |
%f |
小数以下的秒(6 digits for printing: 000000 .. 999000; 1 - 9 digits for parsing: 0 .. 999999999) |
%H |
小时(00 .. 23) |
%h |
小时(01 .. 12) |
%I |
小时(01 .. 12) |
%i |
分钟,数字(00 .. 59) |
%j |
一年的第几天(001 .. 366) |
%k |
小时(0 .. 23) |
%l |
小时(1 .. 12) |
%M |
月份名称(January .. December) |
%m |
月份,数字(01 .. 12) |
%p |
AM or PM |
%r |
时间,12小时制(hh:mm:ss followed by AM or PM) |
%S |
秒(00 .. 59) |
%s |
秒(00 .. 59) |
%T |
时间,24小时制(hh:mm:ss) |
%U |
周(00 .. 53), 星期天是一周的第一天 |
%u |
周(00 .. 53), 星期一是一周的第一天 |
%V |
周(01 .. 53),星期天是一周的第一天,与%X配合使用 |
%v |
星期(01 .. 53), 第一条为星期一,与%X配合使用 |
%W |
周几(Sunday .. Saturday) |
%w |
本周的第几天(0 .. 6),星期天是一周的第一天 |
%X |
年份,数字,4位,第一天为星期日 |
%x |
年份,数字,4位,第一天为星期一 |
%Y |
年份,数字,4位 |
%y |
年份,数字,2位,表示年份范围为[1970, 2069] |
%% |
表示字符'%' |
示例:
select date_format(timestamp '2020-07-25 15:04:00.124','一年的第%j天,%m月的第%d天,%p %T %W'); _col0 --------------------------------------------------- 一年的第207天,07月的第25天,PM 15:04:00 Saturday (1 row)
这些格式化描述符现在还不支持:%D、%U、%u、%V、%w、%X。
- date_format(timestamp, format) → varchar
- date_parse(string, format) → timestamp
select date_parse('2020/07/20', '%Y/%m/%d');-- 2020-07-20 00:00:00.000
Java 日期函数
在这一章节中使用的格式化字符串都是与Java的SimpleDateFormat样式兼容的。
常用提取函数
域 |
描述 |
---|---|
YEAR |
year() |
QUARTER |
quarter() |
MONTH |
month() |
WEEK |
week() |
DAY |
day() |
DAY_OF_MONTH |
day_of_month() |
DAY_OF_WEEK |
day_of_week() |
DOW |
day_of_week() |
DAY_OF_YEAR |
day_of_year() |
DOY |
day_of_year() |
YEAR_OF_WEEK |
year_of_week() |
YOW |
year_of_week() |
HOUR |
hour() |
MINUTE |
minute() |
SECOND |
second() |
TIMEZONE_HOUR |
timezone_hour() |
TIMEZONE_MINUTE |
timezone_minute() |
例如:
select second(timestamp '2020-02-12 15:32:33.215');-- 33 select timezone_hour(timestamp '2020-02-12 15:32:33.215');-- 8
- extract(field FROM x) → bigint
select extract(YOW FROM timestamp '2020-02-12 15:32:33.215');-- 2020 select extract(SECOND FROM timestamp '2020-02-12 15:32:33.215');-- 33 select extract(DOY FROM timestamp '2020-02-12 15:32:33.215');--43
函数 |
示例 |
描述 |
---|---|---|
SECONDS_ADD(TIMESTAMP date, INT seconds) |
SELECT seconds_add(timestamp '2019-09-09 12:12:12.000', 10); |
给时间以秒为单位进行加法 |
SECONDS_SUB(TIMESTAMP date, INT seconds) |
SELECT seconds_sub(timestamp '2019-09-09 12:12:12.000', 10); |
给时间以秒为单位进行减法 |
MINUTES_ADD(TIMESTAMP date, INT minutes) |
SELECT MINUTES_ADD(timestamp '2019-09-09 12:12:12.000', 10); |
给时间以分钟为单位进行加法 |
MINUTES_SUB(TIMESTAMP date, INT minutes) |
SELECT MINUTES_SUB(timestamp '2019-09-09 12:12:12.000', 10); |
给时间以分钟为单位进行减法 |
HOURS_ADD(TIMESTAMP date, INT hours) |
SELECT HOURS_ADD(timestamp '2019-09-09 12:12:12.000', 1); |
给时间以小时为单位进行加法 |
HOURS_SUB(TIMESTAMP date, INT hours) |
SELECT HOURS_SUB(timestamp '2019-09-09 12:12:12.000', 1); |
给时间以小时为单位进行减法 |
- last_day(timestamp) -> date
SELECT last_day(timestamp '2019-09-09 12:12:12.000');-- 2019-09-30 SELECT last_day(date '2019-07-09');--2019-07-31
- add_months(timestamp) -> [same as input]
SELECT add_months(timestamp'2019-09-09 00:00:00.000', 11);-- 2020-08-09 00:00:00.000
- next_day() (timestamp, string) -> date
SELECT next_day(timestamp'2019-09-09 00:00:00.000', 'monday');-- 2019-09-16 00:00:00.000 SELECT next_day(date'2019-09-09', 'monday');-- 2019-09-16
- numtoday(integer) -> BIGINT
描述:将传递的整数值转换为day类型,例如BIGINT类型。
SELECT numtoday(2);-- 2