How to change datetime to string in SQLAlchemy query? Last Updated : 28 Feb, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to change DateTime to string in sqlalchemy query in the python programming language. Database used: Installation Syntax to install sqlalchemy and pymysql: pip install sqlalchmey pymysql Note: pymysql is a dependency of sqlalchemy which we need to install for this post First of all, we need to import the module and connect it to the database. Python3 from sqlalchemy import create_engine user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks' engine = create_engine( url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8') connection = engine.connect() table_name = 'to_do_list' Method 1: Using convert function of SQL So, SQL has a convert function that can be used to change datatype of the column. The syntax of the function is : CONVERT(column_name , datatype) This will convert the column to the given datatype and return the values accordingly. The SQL query will look like : SELECT CONVERT(column_name , CHAR) FROM table_name; We are converting to CHAR as it is one of the data types of string in SQL. Example: Python3 from sqlalchemy import create_engine user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks' engine = create_engine( url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8') connection = engine.connect() table_name = 'to_do_list' query = f'SELECT CONVERT(start_datetime,char) FROM {table_name}' result = connection.execute(query) for elem in result: value = elem[0] print(value, type(value)) Output: Method 2: Using the str function of python : In this method, we will write a normal "SELECT" query of SQL, execute it, fetch the result and then apply the str function to change the value. Example: Python3 from sqlalchemy import create_engine user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks' engine = create_engine( url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8') connection = engine.connect() table_name = 'to_do_list' query = f'SELECT start_datetime FROM {table_name}' result = connection.execute(query) for elem in result: value = elem[0] print(value, type(value)) converted_value = str(value) print(converted_value, type(converted_value)) # just a line for much more readable output print("____________________________\n") Output: Comment More infoAdvertise with us Next Article How to change datetime to string in SQLAlchemy query? mycodenotein Follow Improve Article Tags : Python Geeks Premier League Geeks-Premier-League-2022 Python-SQLAlchemy Practice Tags : python Similar Reads How to Convert Timestamp to Datetime in MySQL? In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret 2 min read How to Strip Time Component From Datetime in MySQL? MySQL is a very popular open-source and free database server that is ideal for small and large applications and is a relational database management system where SQL (Structured Query Language) queries or statements are used to manage and manipulate the data. MySQL provides many built-in functions li 4 min read How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s 2 min read How to use sum and order by in SQLAlchemy query? In this article, we are going to see how to perform the sum and count function in SQLAlchemy against a PostgreSQL database in python. SUM and count operations are performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In PostgreSQL, Gr 3 min read Convert datetime to unix timestamp in SQLAlchemy model When dealing with databases date and time are considered to be one of the most important attributes for any entity. With such data, we often encounter some common task of converting DateTime to a Unix timestamp. In this article, we will learn how we can convert datetime to Unix timestamp in SQLAlche 5 min read DateTime Timezone in SQLAlchemy SQLAlchemy is a powerful and popular Python library that provides a flexible and efficient way to interact with databases. It uses the Object-Relational Mapping (ORM)tool, which acts as a bridge between the Python objects and the relational database. SQLALchemy provides a wide range of methods to wo 8 min read How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f 3 min read How to Execute Raw SQL in SQLAlchemy In this article, we will see how to write a Conventional SQL query in SQLAlchemy using text() against a PostgreSQL database in python. Creating table for demonstration Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() func 3 min read Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone. 2 min read Python SQLAlchemy - Write a query where a column contains a substring In this article, we discussed how to extract the column values containing substring in SQLAlchemy against a PostgreSQL database in python. In SQLAlchemy, generic functions like SUM, MIN, MAX, are invoked like conventional SQL functions using the func attribute. Some common functions used in SQLAlch 2 min read Like