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

Similar Reads

SQLAlchemy Core – func Function

It is an object in SQLAlchemy that serves as a namespace for SQL functions....

SQLAlchemy Mathematical Functions

Python sqlalchemy func.avg(column)...

SQLAlchemy Core – String Functions

...

SQLAlchemy Core – Date and Time Functions

...

SQLAlchemy Core – Other Functions

...