简体   繁体   中英

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

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. 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.

You can also order by trunc(ed_date) , and in the select list you can format that however you want:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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