触发器中的字符到数字转换错误

时间:2013-02-09 08:07:38

标签: oracle if-statement plsql triggers to-char

触发器如何在两个时间之间插入?插入时在character to number conversion条件中出现if错误。以下是我的触发器。

create or replace trigger TRI_INSERT
  after insert on stud_details  
  referencing old as old new as new
  for each row
declare
        strTime varchar2(20) := :new.time_stamp;   -- (eg: '02/08/2013 11:09:42 PM')
  begin
        if (to_char(strTime, 'hh24:mi:ss') between '22:00:00' and '23:59:59') then
             insert into stud_clas_details
              (id,
               v-id,
               w-id,
               al_id,
               time,
               Time_Stamp)
             values
               (seq_ve_id.nextval,
                :new.vehicle_id,
                 :new.way_id,
                 'xxxx',
                 strTime,
                 sysdate);
         end if;
 end TRI_INSERT;

3 个答案:

答案 0 :(得分:1)

你不能to_char()一个具有日期格式的varchar2并期望它能够正常工作。

而你应该做

    if (to_char(:new.time_stamp, 'hh24:mi:ss') between '22:00:00' and '23:59:59') then

如果您想以特定格式将时间插入表中,请使用

to_char(:new.time_stamp, 'hh24:mi:ss')

一样
strTime varchar2(20) := :new.time_stamp;

您只需在该会话的默认NLS_DATE_FORMAT中插入日期(每个会话可能会有所不同)。

答案 1 :(得分:1)

如何使用:

if extract(hour from :new.time_stamp) in (22,23) then ...

答案 2 :(得分:0)

if条件下的问题..现在工作正常..谢谢大家。

创建或替换触发器TRI_INSERT

  after insert on stud_details  

  referencing old as old new as new

  for each row

声明

    strTime varchar2(20) := :new.time_stamp;   -- (eg: '02/08/2013 11:09:42 PM')
    strFromTime varchar2(20):= '22:00:00'
    strToTime varchar2(20):= '23:59:59'

开始

    if ((to_char(strTime, 'hh24:mi:ss') > strFromTime) and (to_char(strTime,           
         'hh24:mi:ss') < strToTime)) then
         insert into stud_clas_details
          (id,
           v-id,
           w-id,
           al_id,
           time,
           Time_Stamp)
         values
           (seq_ve_id.nextval,
            :new.vehicle_id,
             :new.way_id,
             'xxxx',
             strTime,
             sysdate);
     end if;

结束TRI_INSERT;