How to get float values from sql query

I have following table
d dd
______ __
12.345 12
34.567 34
45.678 45
ss = fetch(conn,'SELECT d FROM sc WHERE dd = 12')
when I execute above query, I am getting correct result as
d
______
12.345
but following query giving the wrong answer as an empty table
ss = fetch(conn,'SELECT d FROM sc WHERE d = 12.345')
How to resolve this issue

4 件のコメント

Rik
Rik 2018 年 9 月 16 日
I suspect floats are to blame. If that's the case, it matters a lot where the rounding is taking place, so solving the issue could be very difficult.
Walter Roberson
Walter Roberson 2018 年 9 月 16 日
I suggest pulling out all of d and subtracting 12.345 and seeing what value you get.
Ming Yue
Ming Yue 2018 年 9 月 19 日
Hi Manohar,
What version of MATLAB, what database and which version of connector are you using? I think the issue could be platform dependent.
I tested the above scenario on MATLAB R2018a with MySQL 8.0 and ODBC connector 5.3. Here's what I got:
>> ss = fetch(conn,'SELECT d FROM sc WHERE dd = 12')
ss =
1×1 cell array
{[12.3450]}
>> ss = fetch(conn,'SELECT d FROM sc WHERE d = 12.345')
ss =
1×1 cell array
{[12.3450]}
>> ss = fetch(conn,'SELECT d FROM sc WHERE d = 12.3456')
ss =
[]
Walter Roberson
Walter Roberson 2018 年 9 月 19 日
My suspicion is that the data stored is not bit-for-bit identical to however sql translates decimal 12.345 (keeping in mind it is being transferred as text for the purposes of the query). I think the stored 12.345-ish value might be truncated due to printing limitations.
That is why I recommend pulling back whatever is there and subtract 12.345 to see what the difference is

サインインしてコメントする。

回答 (0 件)

カテゴリ

製品

リリース

R2018a

タグ

質問済み:

2018 年 9 月 16 日

コメント済み:

2018 年 9 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by