Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select date from timestamp in SQLAlchemy

I'm using SQLAlchemy with python and i want to select date from column type timestamp, to do this query:

SELECT DATE(`record_date`) FROM Users

I made this code by sql alchemy but it will return the timestamp:

session.query(Users.record_date).all()

How can i do it?

like image 963
Mohamed Abd El Raouf Avatar asked Dec 01 '25 20:12

Mohamed Abd El Raouf


1 Answers

give an alias

  SELECT DATE(`record_date`) as record_d FROM Users

then use this

 session.query(Users.record_d).all()

If want do it directly then ,try this

your_dates = session.query(cast(Users.record_date, DATE)).all()

EDIT:

your_dates = session.query(func.DATE(Users.record_date)).all()
like image 86
echo_Me Avatar answered Dec 03 '25 09:12

echo_Me



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!