case cast (substring (cast(<date field> as varchar(10)),6,2) as integer)
when between 1 and 3 then 1
when between 4 and 6 then 2
when between 7 and 9 then 3
else 4
end
with t as (select date '2016-08-27' as dt)
select add_months(trunc(dt,'MM'),-(month(dt)-1)%3) from t
;
2016-07-01 这里还有两个选择
with t as (select date '2016-08-27' as dt)
select trunc(add_months(dt,-(month(dt)-1)%3),'MM')
from t
;
2016-07-01
with t as (select date '2016-08-27' as dt)
select add_months(trunc(dt,'YY'),cast((month(dt)-1) div 3 * 3 as INT))
from t
;
2016-07-01 对于早期版本
with t as (select '2016-08-27' as dt)
select printf('%04d-%02d-%02d',year(dt),(((month(dt)-1) div 3) * 3) + 1,1)
from t
2016-07-01 同样,但今天
with t as (select from_unixtime(unix_timestamp(),'yyyy-MM-dd') as today)
select printf('%04d-%02d-%02d',year(today),(((month(today)-1) div 3) * 3) + 1,1) as
from t
2条答案
按热度按时间4si2a6ki1#
如果不能使用dudu建议的各种date函数,则可以将其转换为字符串,解析出月份,并使用case语句。根据您的配置单元版本,您可能需要使用简单的案例而不是搜索。
(假设yyyy-mm-dd)
很难看,但应该管用。
q0qdq0h22#
这似乎是最干净的方法
2016-07-01
这里还有两个选择
2016-07-01
2016-07-01
对于早期版本
2016-07-01
同样,但今天
2017-04-01