# SQL format for converting to microseconds?

**URL:** <https://community.questdb.com/t/sql-format-for-converting-to-microseconds/82>\
**Category:** Community\
**Tags:** sql\
**Created:** [July 9, 2024, 10:52am UTC](https://community.questdb.com/t/sql-format-for-converting-to-microseconds/82 "2024-07-09T10:52:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tolland](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/tolland/32/46_2.png) [@tolland](https://community.questdb.com/u/tolland)\
**Post date:** [July 9, 2024, 10:52am UTC](https://community.questdb.com/t/sql-format-for-converting-to-microseconds/82/1 "2024-07-09T10:52:51Z")

</div>

I followed [this tutorial](https://questdb.io/blog/2021/11/01/plotly-finnhub-realtime-dashboard/) and noticed that my timestamps were wrong in the database;

```auto
"stock_symbol","current_price","high_price","low_price","open_price","percent_change","tradets","ts"
"EBAY",52.79,52.915,52.39,52.81,52.87,"1969-12-31T23:49:51.502912Z","2024-07-09T10:45:32.955649Z"

```

(notice the `1969-12-31` in the tradets value which should be the last traded timestamp, i.e. very recently)

That tutorial is using this code to build the query

```python
    query = f"""
    INSERT INTO quotes(stock_symbol, current_price, high_price, low_price, open_price, percent_change, tradets, ts)
    VALUES(
        '{symbol}',
        {quote["c"]},
        {quote["h"]},
        {quote["l"]},
        {quote["o"]},
        {quote["pc"]},
        {quote["t"]} * 1000000,
        systimestamp()
    );
    """

```

which produces something like:

```sql
    INSERT INTO quotes(stock_symbol, current_price, high_price, low_price, open_price, percent_change, tradets, ts)
    VALUES(
        'DOCN',
        34.67,
        35.11,
        34.21,
        34.72,
        34.72,
        1720468802 * 1000000,
        systimestamp()
    );

```

but that is clearly not converting from epoch seconds to microseconds correctly.

I tried something like this;

```sql
SELECT
  to_utc(1720468802000000, 'Europe/Berlin'),
  to_utc(1720468802 * 1000000, 'Europe/Berlin'),
  to_utc(1720468802, 'Europe/Berlin');

```

and it produces

```auto
"to_utc","to_utc1","to_utc2"
"2024-07-08T18:00:02.000000Z","1969-12-31T22:49:52.502912Z","1969-12-31T23:28:40.468802Z"

```

which is confusing, because it’s now not clear what `1720468802 * 1000000` is actually doing. it doesn’t produce an error

---

<div class="post-metadata">

**Author:** ![tolland](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/tolland/32/46_2.png) [@tolland](https://community.questdb.com/u/tolland)\
**Post date:** [July 9, 2024, 10:55am UTC](https://community.questdb.com/t/sql-format-for-converting-to-microseconds/82/2 "2024-07-09T10:55:52Z")

</div>

Ah I see.

 ![2024-07-09_11-54](https://canada1.discourse-cdn.com/flex004/uploads/questdb/original/1X/91e76255377f471caad2784e594d6db4cbfde08a.png)

Its interpreting it as an int and its getting rolled over

`SELECT 1720468802 * 1000000L;`

this works ok.

---

<div class="post-metadata">

**Author:** ![nwoolmer](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/nwoolmer/32/24_2.png) [@nwoolmer](https://community.questdb.com/u/nwoolmer)\
**Post date:** [July 9, 2024, 11:02am UTC](https://community.questdb.com/t/sql-format-for-converting-to-microseconds/82/3 "2024-07-09T11:02:08Z")

</div>

Hi @tolland ,

Thanks for the report. In this instance, the multiplication gets compiled to a `MulIntFunctionFactory` which can suffer overflow. It should really use longs, since these are timestamps.

We instead need to compile it to `MulLongFunctionFactory`.

Thank you for the report, I will raise a bug issue for it.

Regarding that post, its a few years old now, so we should probably review it for correctness!

EDIT: [friction when converting timestamps due to numeric overflow · Issue #4752 · questdb/questdb · GitHub](https://github.com/questdb/questdb/issues/4752)

---

<div class="post-metadata">

**Author:** ![nwoolmer](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/nwoolmer/32/24_2.png) [@nwoolmer](https://community.questdb.com/u/nwoolmer)\
**Post date:** [September 2, 2024, 11:09am UTC](https://community.questdb.com/t/sql-format-for-converting-to-microseconds/82/4 "2024-09-02T11:09:49Z")

</div>

This has now been resolved and will be released soon. We aim that constant folds such as this one will not implicitly overflow, but instead be treated the same as if they were the raw, already folded value. In this case, that would mean it will be automatically casted to a `LONG`.
