SQLAlchemy Core β Date and Time Functions
Python sqlalchemy func.now()
Returns todays date and current time as result. This code selects the current timestamp using func.now() and labels it as βnowβ. It then executes the query, retrieves the result, and prints the current timestamp, which represents the current date and time.
Python3
query = select(func.now().label( "now" )) with engine.connect() as connect: result = connect.execute(query).fetchall() for data in result: print (data[ 0 ]) |
Output
Python sqlalchemy func.current_time()
Returns the current time. This code selects the current date and time using func.current_date() and func.current_time(). It labels the date as βdateβ and the time as βtimeβ. It then executes the query, retrieves the result, and prints the current date and time.
Python3
query = select(func.current_date().label( "date" ),func.current_time().label( "time" )) with engine.connect() as connect: result = connect.execute(query).fetchall() for data in result: print (data[ 0 ],data[ 1 ]) |
Output:
SQLAlchemy Core β Functions
SQLAlchemy provides a rich set of functions that can be used in SQL expressions to perform various operations and calculations on the data. SQLAlchemy provides the Function API to work with the SQL functions in a more flexible manner. The Function API is used to construct SQL expressions representing function calls and can be applied to columns. SQL functions are invoked by using the func namespace.
Prerequisites