Forum Discussion
How to Convert String time column to timestamp or DATE DATATYPE using
- 2 months ago
If you're working in Fabric using Spark or a notebook, you can convert a string column to either a DATE or TIMESTAMP using the built-in conversion functions.
For example, if your source column contains values such as '2026-07-02':
SQL
SELECT
TO_DATE(date_string_col, 'yyyy-MM-dd') AS converted_date
FROM source_table;
Show more lines
If the source value includes time, such as '2026-07-02 14:30:00', use:
SQL
SELECT
TO_TIMESTAMP(date_string_col, 'yyyy-MM-dd HH:mm:ss') AS converted_timestamp
FROM source_table;
Show more lines
In PySpark:
Python
from pyspark.sql.functions import to_date, to_timestamp
df = df.withColumn(
"DateColumn",
to_date("StringDateColumn", "yyyy-MM-dd")
)
df = df.withColumn(
"TimestampColumn",
to_timestamp("StringTimestampColumn", "yyyy-MM-dd HH:mm:ss")
)
Show more lines
Make sure the format string matches the actual values in your source column. If the source data contains formats such as MM/dd/yyyy or dd-MM-yyyy, adjust the pattern accordingly. You can then write the transformed data into your Silver table with the target column defined as DATE or TIMESTAMP.
- 2 months ago
Hi Koritala,
You can't directly load a string into a DATE/TIMESTAMP column — you need to cast it during the SELECT/transform step before writing to Silver. Just make sure the source string format matches the pattern you pass to the conversion function.
Spark SQL
SELECT to_date(order_date_str, 'yyyy-MM-dd') AS order_date, to_timestamp(order_ts_str, 'yyyy-MM-dd HH:mm:ss') AS order_ts FROM bronze.ordersPySpark
from pyspark.sql.functions import to_date, to_timestamp silver_df = ( bronze_df .withColumn("order_date", to_date("order_date_str", "yyyy-MM-dd")) .withColumn("order_ts", to_timestamp("order_ts_str", "yyyy-MM-dd HH:mm:ss")) ) silver_df.write.mode("append").saveAsTable("silver.orders")
If your source format is different (e.g., dd/MM/yyyy or MM-dd-yyyy HH:mm), adjust the pattern accordingly — otherwise you'll get NULLs. Exact syntax can vary slightly by platform (Fabric / Databricks / Synapse), so if you share your source string format and target platform, I can tailor it further.Hope this helps!
If this got you what you needed, a Kudos and an Accepted Solution mark would be great — it helps others searching for the same thing find the answer quicker.
Disclosure: My earlier response above was drafted with the assistance of an AI tool (Microsoft Copilot) and cross-checked against Microsoft Learn documentation. Sharing this note for transparency as per the Community AI Usage Policy - 1 month ago
Hi Koritala
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. And, if you have any further query do let us know.
Thank you.
If you're working in Fabric using Spark or a notebook, you can convert a string column to either a DATE or TIMESTAMP using the built-in conversion functions.
For example, if your source column contains values such as '2026-07-02':
SQL
SELECT
TO_DATE(date_string_col, 'yyyy-MM-dd') AS converted_date
FROM source_table;
Show more lines
If the source value includes time, such as '2026-07-02 14:30:00', use:
SQL
SELECT
TO_TIMESTAMP(date_string_col, 'yyyy-MM-dd HH:mm:ss') AS converted_timestamp
FROM source_table;
Show more lines
In PySpark:
Python
from pyspark.sql.functions import to_date, to_timestamp
df = df.withColumn(
"DateColumn",
to_date("StringDateColumn", "yyyy-MM-dd")
)
df = df.withColumn(
"TimestampColumn",
to_timestamp("StringTimestampColumn", "yyyy-MM-dd HH:mm:ss")
)
Show more lines
Make sure the format string matches the actual values in your source column. If the source data contains formats such as MM/dd/yyyy or dd-MM-yyyy, adjust the pattern accordingly. You can then write the transformed data into your Silver table with the target column defined as DATE or TIMESTAMP.