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

Oracle 触发器中的字符到数字转换错误,oracle,if-statement,plsql,triggers,to-char,Oracle,If Statement,Plsql,Triggers,To Char,触发器将如何在两次计时之间插入?插入时在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:0

触发器将如何在两次计时之间插入?插入时在
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;
您不能
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_格式插入日期(每个会话可能会有所不同)。

使用以下格式如何:

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

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

创建或替换触发器TRI_插件

  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)和(to_char(strTime,
“hh24:mi:ss”)然后
插入螺柱类零件
(id),
v-id,
w-id,
阿尔尤德,
时间
时间戳)
价值观
(seq_ve_id.nextval),
:new.vehicle\u id,
:new.way\u id,
“xxxx”,
时间,
系统日期);
如果结束;

端部三U形嵌件

所有这些都假定
螺柱详细信息的
时间戳
列具有类型
时间戳
日期
。鉴于名称,人们希望如此,但鉴于上面的PL/SQL,最好先检查。我想插入数据,前提是:new_time_stamp的计时时间在22:00和23:59之间。@VasuSambandham是的,if检查会这样做(一个字符比较,但在这种情况下可以安全地执行)
stud_details.time_stamp的类型是什么?如果它是
char
varchar
的某个变体,为什么?stud\u details.time\u stamp是timestamp datatype.Nice。我经常忘记日期和时间特定的功能。但使用内置函数通常更清晰,也更可能正确,而不是通过
将片段提取到\u char
并使用这些函数。
    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;