我在将varchar2
值转换或转换为数字时遇到问题。问题是我的TO_EMPLOYEE
列(在PROJ_NOTIFY_HIST
中)同时包含电子邮件地址和员工ID。
SELECT NOTIFY.PROJ_ID
FROM PROJ_NOTIFY_HIST NOTIFY
WHERE NOTIFY.NOTIFIED_SENT = 0
AND CAST(NOTIFY.TO_EMPLOYEE AS NUMBER) NOT IN (SELECT EMPLOYEE_ID
FROM V_ACTIVE_EMPLOYEE_INFO);
有什么方法可以只获取员工ID,并将其与我的子查询进行比较?
答案 0 :(得分:0)
我怀疑您想要这样的东西:
SELECT NOTIFY.PROJ_ID
FROM PROJ_NOTIFY_HIST NOTIFY
WHERE NOTIFY.NOTIFIED_SENT = 0 AND
NOT EXIST (SELECT 1
FROM V_ACTIVE_EMPLOYEE_INFO A
WHERE TO_CHAR(A.EMPLOYEE_ID) = NOTIFY.TO_EMPLOYEE
);
主要的东西?强制转换为字符串而不是数字。
如果只想将其限制为看起来像数字的值,则:
SELECT NOTIFY.PROJ_ID
FROM PROJ_NOTIFY_HIST NOTIFY
WHERE NOTIFY.NOTIFIED_SENT = 0 AND
REGEXP_LIKE(EMPLOYEE_ID, '^[0-9]+$') AND
NOT EXIST (SELECT 1
FROM V_ACTIVE_EMPLOYEE_INFO A
WHERE TO_CHAR(A.EMPLOYEE_ID) = NOTIFY.TO_EMPLOYEE
);
答案 1 :(得分:0)
您可以测试每个to_employee
值以查看它是否仅由数字字符组成,然后才尝试将其转换为数字并与您的视图进行比较。在最新版本的Oracle中,您可以仅使用to_number()
并使用其default ... on error
子句来阻止ORA-01722。或validate_conversion()
进行测试。
但这只会告诉您该值是否为数字,而不是是否为员工ID,然后您可以检查该ID是否处于活动状态。您可以在其中输入其他不应该是ID的号码,例如电话号码。
如果您有一个包含所有员工ID的表格,而不仅仅是活动的ID,则可以用其他方式(从数字到字符串)进行转换以查找匹配项;例如如果您有一个employee_info
表,其中包含所有活动和不活动的员工,则类似:
select notify.proj_id
from proj_notify_hist notify
join employee_info e
on notify.to_employee = cast(e.employee_id as varchar2(10)) -- use suitable size
where notify.notified_sent = 0
and e.employee_id not in (select employee_id
from v_active_employee_info);
或
select notify.proj_id
from proj_notify_hist notify
join employee_info e
on notify.to_employee = cast(e.employee_id as varchar2(10)) -- use suitable size
where notify.notified_sent = 0
and not exists (select null
from v_active_employee_info active
where active.employee_id = e.employee_id);
如果v_active_employee_info
是employee_info
表上的视图,则可以跳过not in
或exists
查找,而是直接测试视图使用的任何条件过滤在职员工。
答案 2 :(得分:-2)
用例指导检查是否应进行转换(字段由数字组成)。
CASE [ expression ]
WHEN condition_1 THEN result_1
ELSE result
END