cancel
Showing results for 
Search instead for 
Did you mean: 

Bypassdata conversion in snowflake python connector

digitalspecz
New Contributor
Have a question regarding data conversion in snowflake connector for python. I have some timestamp data in Snowflake and I am retrieving it with python connector. I want the timestamp data to be in string format not in datetime.datetime format.
 
1 REPLY 1

abiel_bermudez
RUCKUS Team Member

Hi digitalspecz , thanks for your question!

To convert a timestamp in datetime.datetime format to a string format in Python, you can use the strftime() method. Here's an example:

 

import snowflake.connector

# Establish a connection to Snowflake
conn = snowflake.connector.connect(
user='YOUR_USER',
password='YOUR_PASSWORD',
account='YOUR_ACCOUNT',
warehouse='YOUR_WAREHOUSE',
database='YOUR_DATABASE',
schema='YOUR_SCHEMA'
)

# Create a cursor object
cursor = conn.cursor()

# Retrieve the timestamp data as datetime objects
cursor.execute('SELECT timestamp_col FROM your_table')

# Retrieve the timestamp data as strings
timestamp_data = [row[0].strftime('%Y-%m-%d %H:%M:%S.%f') for row in cursor.fetchall()]

# Close the cursor and connection
cursor.close()
conn.close()

 

The strftime() method is used to convert the datetime.datetime object to a string format.

You can adjust the format string to match the specific string format you want to convert the timestamp to.