Ecto query for date range

Zubair Nabi

I have to query the date range on the basis of timestamps in the model. what i have tried so far is

str="2017-12-18"
{:ok, ed}=Ecto.Date.cast(str)
ed= ed |> Ecto.Date.from_erl  |> NaiveDate.from_erl!
Repo.all(from l in Log, where: l.inserted_at == ^ed)

However the date in the postgres is in the form of datetime, e.g. 2017-12-18 19:58:01.414.

but i just want to query on the date and not the time. However if i use Ecto.DateTime and NaiveDateTime and pass the acurate datetime in string, it works. but i just want to match on the date, instead. How can that be possible? Thanks

Dogbert

You can cast the field to a date using a fragment and then compare that:

Repo.all(from l in Log, where: fragment("?::date", l.inserted_at) == ^ed)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related