我有以下步骤,要将字符串数据转换为日期并将其与其他日期进行比较。
eev.screen_entry_value
是一个Varchar2,其值的格式为“ 2018/10/26 00:00:00”。
我想将此转换为日期格式,并与以下其他日期进行比较-
TO_DATE(to_date(eev.screen_entry_value,'YYYY/MM/DD HH24:MI:SS'),'DD-MON-YYYY') BETWEEN P_START_DATE AND P_END_DATE;
但是,我遇到一个错误-
ORA-01861:文字与格式字符串不匹配
下面是我的代码段-
declare
p_start_date DATE;
p_end_date DATE;
p_screen_entry_value varchar2(60);
BEGIN
SELECT ptp2.start_date, ptp2.end_date
into p_start_date, p_end_date
FROM per_time_periods ptp
, per_time_periods ptp2
, pay_payrolls_f pp
WHERE pp.payroll_name LIKE 'US Semi-Monthly'
AND TRUNC(sysdate) BETWEEN pp.effective_start_date AND pp.effective_end_date
AND pp.payroll_id = ptp.payroll_id
AND TRUNC(sysdate) BETWEEN ptp.START_DATE AND ptp.END_DATE
AND ptp.payroll_id = ptp2.payroll_id
AND ptp.end_date BETWEEN ptp2.start_date AND ptp2.end_date;
dbms_output.put_line(p_start_date);
dbms_output.put_line(p_end_date);
SELECT eev.screen_entry_value
into p_screen_entry_value
FROM pay_input_values_f piv,
pay_element_types_f et,
pay_element_links_f el,
pay_element_entries_f ee,
pay_element_entry_values_f eev
WHERE
ee.assignment_id IN (364018)
AND ee.element_link_id = el.element_link_id
AND sysdate BETWEEN el.effective_start_date
AND el.effective_end_date
AND ee.element_type_id = et.element_type_id
AND sysdate BETWEEN et.effective_start_date
AND et.effective_end_date
AND et.attribute_category = 'US Elements'
AND et.attribute1 = 'Y' --Interface to ProBusiness
AND ee.element_entry_id = eev.element_entry_id
AND sysdate BETWEEN eev.effective_start_date
AND eev.effective_end_date
AND eev.input_value_id = piv.input_value_id
AND sysdate BETWEEN piv.effective_start_date
AND piv.effective_end_date
AND eev.screen_entry_value IS NOT NULL
AND et.element_name IN ('HV Sign On Bonus','HV Retention Bonus','Relocation') --XX_HR_NONREC_ELEMENTS
AND piv.name IN('Vested 1 Date');
--AND TO_DATE(to_date(eev.screen_entry_value,'YYYY/MM/DD HH24:MI:SS'),'DD-MON-YYYY') BETWEEN P_START_DATE AND P_END_DATE;
dbms_output.put_line(p_screen_entry_value);
end;
答案 0 :(得分:2)
执行to_date(to_date(...))
并没有任何意义。内部的将字符串转换为日期,然后外部的将字符串转换为日期。为此,它必须再次将日期隐式转换回字符串,并且它将在该隐式步骤中使用会话的NLS_DATE_FORMAT
。
如果您想将其恢复为特定格式的字符串,则可以使用to_char()
进行外部调用:
to_char(to_date(eev.screen_entry_value,'YYYY/MM/DD HH24:MI:SS'),'DD-MON-YYYY') ...
但是您要与之比较的值是日期,因此您根本不需要或完全不需要第二次转换:
to_date(eev.screen_entry_value,'YYYY/MM/DD HH24:MI:SS')
BETWEEN P_START_DATE AND P_END_DATE