ZeroITStudy
-- 时间戳转日期
-- yyyyMMdd格式:
select from_unixtime(clktime/1000,\'yyyyMMdd\') as date_    --20210101
from table
where clktime=1609470888000  --毫秒

-- yyyy-MM-dd格式:
select from_unixtime(clktime/1000,\'yyyy-MM-dd\') as date_    --2021-01-01
from table
where clktime=1609470888000  --毫秒

-- yyyy-MM-dd HH:mm:ss格式:
select from_unixtime(clktime/1000,\'yyyy-MM-dd HH:mm:ss\') as date_    --2021-01-01 11:14:48
from table
where clktime=1609470888000  --毫秒


-- 日期转时间戳
select unix_timestamp(\'2021-01-01\',\'yyyy-MM-dd\') as date_    -- 1609430400   
from tablle

select unix_timestamp(\'20210101\',\'yyyyMMdd\') as date_    -- 1609430400
from table

select unix_timestamp(\'2021-01-01 11:14:48\',\'yyyy-MM-dd HH:mm:ss\') as date_    -- 1609470888
from table


-- 日期转转时间戳单位为秒:unix_timestamp(\'2021-01-01\',\'yyyy-MM-dd\')
-- 日期转转时间戳单位为毫秒:unix_timestamp(\'2021-01-01\',\'yyyy-MM-dd\')/1000
-- 日期转时间戳unix_timestamp(\'日期\',\'格式\')中的日期与格式必须一致,否则转化结果为null


-- 获取当前日期
select from_unixtime(unix_timestamp(),\'yyyy-MM-dd HH:mm:ss\') as date_    -- 2021-02-02 16:01:54
from table

select current_timestamp    -- 2021-02-02 16:01:54
from table

select from_unixtime(unix_timestamp(),\'yyyy-MM-dd\') as date_    -- 2021-02-02
from table

-- 返回具体的日期(年-月-日)    
select to_date(\'2021-02-02 16:01:54\') as date_    -- 2021-02-02
from table

-- 返回年
select year(\'2021-02-02 10:03:01\') as date_    -- 2021
from table

-- 返回月
select month(\'2021/02/02 10:03:01\') as date_    -- 2
from table

-- 返回日
select day(\'2021-02-02 10:03:01\') as date_    -- 2
from table

-- 返回时
select hour(\'2021-02-02 10:03:01\') as date_    -- 10
from table

-- 返回分
select minute(\'2021-02-02 10:03:01\') as date_    -- 3
from table

-- 返回秒
select second(\'2021-02-02 10:03:01\') as date_    -- 1
from table

-- 返回日期在当年的周数
select weekofyear(\'2021-02-02 10:03:01\') as date_    -- 5
from table

-- 返回结束日期减去开始日期的天数
select datediff(\'2021-02-02\',\'2021-01-02\') as date_    -- 31
from table

-- 返回起始日期增加n天后的日期
select date_add(\'2021-02-02\',10) as date_    -- 2021-02-12
from table

-- 返回起始日期减去n天后的日期
select date_sub(\'2021-02-02\',10) as date_    -- 2021-01-23
from table

-- 返回当前月份的第一天
select trunc(\'2021-02-02\',\'MM\') as date_    -- 2021-02-01
from table

-- 返回当前月份的最后一天
select last_day(\'2021-02-02 10:03:01\') as date_    -- 2021-02-28
from table

-- 返回当年的第一天
select trunc(\'2021-02-02\',\'YEAR\') as date_    -- 2021-01-01
from table

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-11-18
  • 2021-11-18
猜你喜欢
  • 2021-07-25
  • 2021-12-24
  • 2021-07-29
  • 2022-02-27
相关资源
相似解决方案