SQL 时间计算

select from_unixtime(unix_timestamp(to_date(now()),'yyyy-mm-dd'),'yyyymmdd');

--时间差(转化为小时,保留两位小数)

select round((unix_timestamp('2022-02-23 01:30:30') - unix_timestamp('2022-02-22 00:00:00')) / 3600,2);

--本周

select weekofyear('2022-02-20');

--昨天

date_sub(now(),1)

--前天

date_sub(now(),2)

--本周

select create_time>date_sub(next_day(now(),'MO'),7);

--上周

select create_time>date_sub(next_day(now(),'MO'),14)

and create_time

--本月

select substr(create_time,0,4)=year(now())

and substr(create_time,6,2)=month(now());

--上月

select substr(create_time,0,4)=year(add_months(now(),-1))

and substr(create_time,6,2)=month(add_months(now(),-1));

--本季度

select substr(create_time,0,4)=year(now())

and quarter(create_time)=quarter(now())

--上季度

select substr(create_time,0,4)=year(now())

and quarter(create_time)=quarter(add_months(now(),-3))

--本半年

select substr(create_time,0,4)=year(now());

and substr(create_time,6,2) >= case when month(now()) <= 6 then 1 else 7 end

and the_period <= case when month(date_sub(now(),1)) <= 6 then 6 else 12 end

--今年

select substr(create_time,0,4)=year(now());

你可能感兴趣的:(sql,数据库,database)