Classifying the values present in a single column using SQL into their respective datatypes?

193 views Asked by At

I have been trying to classify the a set values present in a single column (just like above) into datatypes. The problem being that i am using Aster SQL environment (availability of function and the environment as a whole is very limited). Another problem is there are a lot of junk values in the column, a lot of symbols, characters etc. which makes it hard to even hard-code the problem. The structure is something like:

FeatureValue
123
24
15.6
17:15
abc
12/18/2014
17/222222           
abc1200                                
001001oo             
positve+              
+1                           

I would like the solution to be a SQL query. The end result should be something like:

FeatureValue    Type
123             Numeric
24              Numeric
15.6            Numeric
17:15           String (?time)
abc             String
12/18/2014      Date
17/222222       String
abc1200         String
001001oo        String
positve+        String
+1              String

I coded a little, but this solution is not very reliable. What I did was:

case 
            when upper(trim(feature_value)) not like '%A%' and
            upper(trim(feature_value)) not like '%B%' and
            upper(trim(feature_value)) not like '%C%' and
            upper(trim(feature_value)) not like '%D%' and
            upper(trim(feature_value)) not like '%E%' and
            upper(trim(feature_value)) not like '%F%' and
            upper(trim(feature_value)) not like '%G%' and
            upper(trim(feature_value)) not like '%H%' and
            upper(trim(feature_value)) not like '%I%' and
            upper(trim(feature_value)) not like '%J%' and
            upper(trim(feature_value)) not like '%K%' and
            upper(trim(feature_value)) not like '%L%' and
            upper(trim(feature_value)) not like '%M%' and
            upper(trim(feature_value)) not like '%N%' and
            upper(trim(feature_value)) not like '%O%' and
            upper(trim(feature_value)) not like '%P%' and
            upper(trim(feature_value)) not like '%Q%' and
            upper(trim(feature_value)) not like '%R%' and
            upper(trim(feature_value)) not like '%S%' and
            upper(trim(feature_value)) not like '%T%' and
            upper(trim(feature_value)) not like '%U%' and
            upper(trim(feature_value)) not like '%V%' and
            upper(trim(feature_value)) not like '%W%' and
            upper(trim(feature_value)) not like '%X%' and
            upper(trim(feature_value)) not like '%Y%' and
            upper(trim(feature_value)) not like '%Z%' and       
            upper(trim(feature_value)) <>'' and
            upper(trim(feature_value)) not like '%+%' and 
            upper(trim(feature_value)) is not null and
            --upper(trim(feature_value))<>'-' and 
            upper(trim(feature_value))<>'NULL' and 
            upper(trim(feature_value)) not like '%/%' and 
            upper(trim(feature_value)) not like '%-%' and 
            upper(trim(feature_value)) not like '%:%' and 
            feature_value is not null 
                then 'NUMERIC'           
            else 'STRING'
        end as value_type
1

There are 1 answers

0
Aron Hennerdal On BEST ANSWER

You could try to get the CASE-nightmare a bit more under control with a character range in the LIKE-statement:

CASE WHEN upper(trim(feature_value)) NOT LIKE '%[A-Z/-+:]%'
    AND upper(trim(feature_value)) NOT LIKE ''
    AND upper(trim(feature_value)) IS NOT NULL
    THEN 'NUMERIC'
    ELSE 'STRING'
END AS value_type

Modify/extend as needed.