MySQL日期相关函数

梦莱3年前技术文章1240

一、当前时间

--当前时间
select current_timestamp();
--当前时间 等价于 select current_timestamp();
select now();
--基本等价于以上两个
select sysdate();
--当前时间对应的时间戳
select unix_timestamp();

now() 函数和 current_timestamp() 是一样的 获取的都是 SQL 开始执行时的系统时间

sysdate() 函数是执行此函数时的系统时间

二、时间转换函数

--日期格式化函数
date_format(date, format)
eg:
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s');

--时间格式化函数
time_format(date, format)
eg:
select time_format(now(), '%H:%i:%s');
--注:只能转化时间,无法带日期

--字符串转化
str_to_date(str,format)
eg:
SELECT STR_TO_DATE('3,23,2023','%m,%d,%Y');

三、时间戳转换

--时间转化为时间戳
from_unixtime(timestamp)
eg:
select unix_timestamp('date');
select unix_timestamp('2023-03-27 15:15:50')

--将时间戳转化为日期 (格式:yyyy-mm-dd hh:ii:ss)
from_unixtime(timestamp)
eg:
select from_unixtime(1679901350);

--将时间戳转化为需求格式
from_unixtime(timestamp,format)
eg:
select from_unixtime(unix_timestamp(),'%H:%i:%s');
select from_unixtime(unix_timestamp(),'%Y-%m-%d %H:%i:%s');


相关文章

PG常用命令

1、连库相关#连库 $ psql -h <hostname or ip> -p <端口> [数据库名称] [用户名称] #连库并执行命令 $ psql -h <ho...

prometheus黑盒监控

prometheus黑盒监控

一.背景黑盒监控:主要关注的现象,一般都是正在发生的东西,例如出现一个告警,业务接口不正常,那么这种监控就是站在用户的角度能看到的监控,重点在于能对正在发生的故障进行告警。二.操作前了解相关配置和要求...

HDP-Yarn开启CPU调度和隔离

HDP-Yarn开启CPU调度和隔离

进入到ambari主界面 点击yarn 点击config CPU Scheduling and Isolation 设置为enable修改高级配置点击ADVANCED搜索需要修改的配yarn.node...

Redis Sentinel与Cluster安装部署(一)

Redis Sentinel与Cluster安装部署(一)

1、部署规划版本redis5.0.8目录规划软件安装目录/usr/local/redis5可执行文件目录/usr/local/redis5/bin数据目录/usr/local/redis5/dada/...

MySQL运维实战(7.2) MySQL复制server_id相关问题

MySQL运维实战(7.2) MySQL复制server_id相关问题

主库server_id没有设置主库没有设置server_idGot fatal error 1236 from master when&nb...

CDH实操--修改集群主机名

CDH实操--修改集群主机名

1、停止集群2、停止cmsystemctl stop cloudera-scm-serversystemctl stop cloudera-scm-agnet3、修改服务器主机名修改下列配置文件下的主...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。