SP or func to determine a suitable datatype

  • Does anyone know if there is a system function or stored procedure that given a value it will return a suitable datatype?

    i.e. given 'asdfasdfasdf' it will return a char(12)

    '234' returns an int

    '234.566' returns a numeric or decimal

    Anything like that would be good.


    "I met Larry Niven at ConClave 27...AND I fixed his computer. How cool is that?"
    (Memoirs of a geek)

  • Not that I know of, but you could probably make one. 

    Something like below should do the trick:

     

    select Value,

     Case

      When isnumeric(Value)= 0 and isdate(Value) = 0  then 'char(' + cast(len(Value) as varchar) + ')'

      When isnumeric(Value)=1 and charindex('.', Value) = 0  then 'int'

      When isnumeric(Value)=1 and charindex('.', Value) <> 0 then 'numeric'

      When isdate(Value)=1 then 'date'

     End

    from #Val

     

    Signature is NULL

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply