Search code examples
wolfram-mathematicasqldatetime

How to extract a Date from an SQLDateTime object in Mathematica


I am trying to do a plot of a time series with DateListPlot. I want to feed it a time series I obtain from an SQL database. When I retrieve the time series the list is composed of SQLDateTime entries that DateListPlot doesn't understand.

In[24]:= t=SQLExecute[conn, "select timestamp,value from timeseries order by timestamp asc"]

Out[24]={{SQLDateTime[{2010,1,1}],12.3},{SQLDateTime[{2010,1,2}],12.51}}

Doesn't work: In[25]:= DateListPlot[t]

DateListPlot requires a Date tuple and doesn't understand SQLDateTime. What can I do?


Solution

  • The answer:

    In[1]:= SQLDateTime[{2001, 5, 7}][[1]]
    Out[1]:= {2001,5,7}
    

    Mathematica thinks of everything very similarly internally. What you see as {1, 2, 3} is actually List[1,2,3]. The Part function (denoted by [[...]]) works just as well on any function, not just List.

    The quick and dirty way to apply this in your case:

    {#[[1,1]],#[[2]]}& /@ SQLExecute[...]