Thanks,
Larry...that answers enough questions for me to propose solution that, I believe, resolves your need.
My solution uses a user-defined function, "NUM_CHECK", that I have posted on other occasions here in the Oracle forums. The "NUM_CHECK" function accepts as an input argument, any string. It then returns that string as a NUMBER value if it is a valid number. It the string is not a valid number, the function returns a NULL.
So, in the solution below, I list:[ul][li]the code for NUM_CHECK[/li][li]the contents of a sample table con taining a VARCHAR SSN, "HR_DATA"[/li][li]A proof-of-concept SQL statement that illustrates the SSN validations and the output you were expecting.[/li][/ul]The functional narrative of the SSN-related SQL code (in order of execution) is:[ul][li]
ltrim(ssn) - trim off any leading blank spaces.[/li][li]
length(ltrim(ssn)) - determine the length of the trimmed string.[/li][li]
decode(length(ltrim(ssn))... - Evaluate the length of that string, and...[/li][li]
...9,to_char(num_check(ssn),'fm000000009') - If the length = 9, then execute my "num_check" function, transforming valid numbers into a 9-character numeric result; if, however, the length is not 9, then produce a NULL.[/li][li]
nvl(...,'Invalid SSN: '||ssn) - If the previous result is NULL (due to either length issues or non-numeric-value issues), then output an error message in front of the SSN; otherwise output the valid SSN.[/li][/ul]:
Code:
create or replace function Num_check (x varchar2) return number is
num_hold number;
begin
num_hold := x;
return num_hold;
exception
when others then
return null;
end;
/
Function created.
select * from hr_data;
LAST_NAME SSN
------------------------- ----------
Velasquez 001234567
Ngao 1024691
Nagayama 203703701
Quick-To-See 30493A26A
Ropeburn 406172835
select last_name
,nvl(decode(length(ltrim(ssn)),9,to_char(num_check(ssn),'fm000000009')),'Invalid SSN: '||ssn) SSN
from hr_data
/
LAST_NAME SSN
------------------------- -----------------------
Velasquez 001234567
Ngao Invalid SSN: 1024691
Nagayama 203703701
Quick-To-See Invalid SSN: 30493A26A
Ropeburn 406172835
Let us know if this resolves your need and/or if you have any questions.
![[santa] [santa] [santa]](/data/assets/smilies/santa.gif)
Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services:
www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”