[英]remove time from date in Oracle SQL query
我有一個問題
select count(1) z1
from sales_data
where ed_date >= sysdate -7
我需要按 ed_date 分組,刪除時間戳,以及 ed_date 從 2021 年 1 月 1 日到當前 sysdate 的條件
您可以截斷日期:
select trunc(ed_date, 'DD') as ed_date, count(*) as z1
from sales_data
where ed_date >= date '2021-01-01'
and ed_date < trunc(sysdate, 'DD')
group by trunc(ed_date, 'DD')
'DD'
截斷限制是默認設置,因此如果您願意,可以省略它,就像trunc(ed_date)
一樣。 請注意,盡管它不會刪除時間,它只是將其設置為午夜 - Oracle 日期始終具有日期和時間組件。
您也可以order by trunc(ed_date)
,並且在 select 列表中,您可以隨意格式化:
select to_char(trunc(ed_date), MM/DD/YYYY') as ed_date, count(*) as z1
from sales_data
where ed_date >= date '2021-01-01'
and ed_date < trunc(sysdate) -- to exclude today and future
-- and ed_date < trunc(sysdate) + 1 -- to include today but not tomorrow onwards
group by trunc(ed_date)
order by trunc(ed_date)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.