JuliaDatabases / ODBC.jl

An ODBC interface for the Julia programming language
https://odbc.juliadatabases.org/stable
Other
106 stars 63 forks source link

Documentation: Dec64 type conversion to Integer #288

Closed aalexandersson closed 4 years ago

aalexandersson commented 4 years ago

I am a Julia beginner and do not know how to convert column type Union{Missing, Dec64} to Integer in the output from ODBC. I am using Julia 1.4.2 and ODBC 1.0.0 on Windows 10 with an Oracle database driver.

The result of DBInterface.execute(conn, sql) on my SQL query is ODBC.Cursor{false,false} which at the end includes "metadata" with a 3x7 Array{Any,2} with two columns for ZIPCODE_ZIP and ZIPCODE_COUNTY of column type Union{Missing, Dec64}.

Ideally, I would like to avoid the conversion and if possible simply extract the two Dec64 type columns as Integer.

I tried convert(Int32, get(x.ZIPCODE_ZIP)) (based on https://discourse.julialang.org/t/decfp-type-conversion-to-integer/5752 and https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/) but get error

MethodError: Cannot `convert` an object of type Array{Union{Missing, DecFP.Dec64},1} to an object of type Int32
Closest candidates are:
  convert(::Type{Int32}, !Matched::DecFP.Dec32) at C:\Users\aalexandersson\.julia\packages\DecFP\quvcm\src\DecFP.jl:454
  convert(::Type{Int32}, !Matched::DecFP.Dec64) at C:\Users\aalexandersson\.julia\packages\DecFP\quvcm\src\DecFP.jl:454
  convert(::Type{Int32}, !Matched::DecFP.Dec128) at C:\Users\aalexandersson\.julia\packages\DecFP\quvcm\src\DecFP.jl:454

Having a documented example of how to convert the data type or to get the wanted data type directly would be very helpful to me as a beginner for using ODBC.

Edit: I think display formatting is what I want, rather than conversion. This is the easiest solution I found to get rid of the decimals:

using PrettyTables
pretty_table(x, formatters = ft_printf("%.0f"))
quinnj commented 4 years ago

Thanks for opening an issue! I added some example docs in the "Getting Started" section of the docs; hopefully that helps.