简体   繁体   English

从 Oracle SQL 查询中删除时间

[英]remove time from date in Oracle SQL query

i have a query我有一个问题

select count(1) z1
        from sales_data
        where ed_date >= sysdate -7

i need to group by ed _date, remove the timestamp, and condition where ed_date is from 1/1/2021 to current sysdate我需要按 ed_date 分组,删除时间戳,以及 ed_date 从 2021 年 1 月 1 日到当前 sysdate 的条件

You can truncate the date :您可以截断日期

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')

The 'DD' truncation limit is the default so you could omit that, as just trunc(ed_date) , if you prefer. 'DD'截断限制是默认设置,因此如果您愿意,可以省略它,就像trunc(ed_date)一样。 Note though that it doesn't remove the time, it just sets it to midnight - an Oracle date always has both date and time components.请注意,尽管它不会删除时间,它只是将其设置为午夜 - Oracle 日期始终具有日期和时间组件。

You can also order by trunc(ed_date) , and in the select list you can format that however you want:您也可以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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM