ORA-01858:查询中的非数字字符问题

时间:2015-12-01 18:01:57

标签: sql oracle

所以我的sql查询是按日期创建分组如下。

select to_date(page_start_time,'DD-MM-YYYY') EventDate , count(case when page_title_to = 'Appointments' then event_id else null end) as Appointments
, count(case when page_title_to = 'User Login' then event_id else null end) as UserLogin
, count(case when page_title_to = 'Store Visit' then event_id else null end) as StoreVisit 
, count(case when page_title_to = 'Resume Store Visit' then event_id else null end) as ResumeStoreVisit 
, count(case when page_title_to = 'Visit History' then event_id else null end) as VisitHistory 
from MUD_SESSION_FLOWS
group by to_date(page_start_time,'DD-MM-YYYY') 
order by to_date(page_start_time,'DD-MM-YYYY') desc

这个失败,异常

ORA-01858: a non-numeric character was found where a numeric was expected

如果我将to_date更改为to_char,那么查询工作正常。但是,它并没有按日期排序,但我想将page_start_time视为字符串值。所以,我没有得到正确的数据顺序。请告知如何修复它以便以正确的顺序订购数据

2 个答案:

答案 0 :(得分:1)

如果您想要将时间戳到目前为止,请使用以太trunc(date)round(date)函数。

答案 1 :(得分:1)

不要将datetimestamp传递给to_date()函数。

如果您想在没有时间的情况下按天分组,请使用trunc()

select to_char(trunc(page_start_time),'DD-MM-YYYY') EventDate, 
       count(case when page_title_to = 'Appointments' then event_id else null end) as Appointments, 
       count(case when page_title_to = 'User Login' then event_id else null end) as UserLogin, 
       count(case when page_title_to = 'Store Visit' then event_id else null end) as StoreVisit, 
       count(case when page_title_to = 'Resume Store Visit' then event_id else null end) as ResumeStoreVisit, 
       count(case when page_title_to = 'Visit History' then event_id else null end) as VisitHistory 
from MUD_SESSION_FLOWS
group by trunc(page_start_time)
order by trunc(page_start_time) desc