SQL: To Identify a non-Numeric value in a Alpha Character Field
Below query will return the records that are having non numeric value in Field1 coumn.
select * from Tbl1 where
translate(Trim(FIELD1), ' X', '1234567890 ') <> ' '
translate(Trim(FIELD1), ' X', ' 1234567890 ')
Trim will remove trailing and leading spaces.
Translate will translate FIELD1 values of 1, 2, 3, 4, ....0 to ' ' as mentioned (10 blanks one for each number, translation happens character by character) and the ' ' will be translated to X. Hence if a record has FIELD1 has numeric value all the numbers will be converted to blanks and ' ' inbetween will be converted to X. hence if there is a record that is non-blank which means it has non-numeric value which the translate couldn't convert
Translate in update:
Field1 = 1O5719374
update TBL1 set FIELD1 = translate(FIELD1, ' ','O')
where
FIELD2 = 15145
FIELD1 = 1 5719374
Now FIELD1 is 1 5719374 which is non numeric if you run below query (Without the like '% %')
this record will be skipped as the non numeric character here is a blank and we are checking FIELD1 after translating to be non blank. here the all the numbers will be converted to blanks finally we will have only blanks (+ already existing non converted blank)
Hence translating the space in the middle to X will avoid that
Translate will translate FIELD1 values of 1, 2, 3, 4, ....0 to ' ' as mentioned (10 blanks one for each number, translation happens character by character) and the ' ' will be translated to X. Hence if a record has FIELD1 has numeric value all the numbers will be converted to blanks and ' ' inbetween will be converted to X. hence if there is a record that is non-blank which means it has non-numeric value which the translate couldn't convert
Translate in update:
Field1 = 1O5719374
update TBL1 set FIELD1 = translate(FIELD1, ' ','O')
where
FIELD2 = 15145
FIELD1 = 1 5719374
Now FIELD1 is 1 5719374 which is non numeric if you run below query (Without the like '% %')
this record will be skipped as the non numeric character here is a blank and we are checking FIELD1 after translating to be non blank. here the all the numbers will be converted to blanks finally we will have only blanks (+ already existing non converted blank)
Hence translating the space in the middle to X will avoid that
select * from Tbl1 where
translate(Trim(FIELD1), ' X', '1234567890 ') <> ' '
Equivalent Query is using trim and '% %' on the same field
This will exclude records with FILED1 having leading and trailing blank
select * from Tbl1 where
TRIM(FIELD1) like '% %' or translate(FIELD1, ' ', '1234567890') <> ' '
Comments
Post a Comment