How to convert Data Chr Strings to Date Strings

Jeff1
Contributor II

Databricks Community

New to Databricks and work in R code. I have a data from with a date field that is a chr string and need to convert to a date field. Tried the standard as.Date(x, format = "%Y-%m-%d") , then tried the dplyr::mutate function and that errored out to. So how does one convert chr date strings to a Date string.

Hubert-Dudek
Databricks MVP

you can use universal sql syntax with R dataframes

df <- read.df(path = "/tmp/df.avro", source = "com.databricks.spark.avro")
createOrReplaceTempView(dfView, "df")
species <- sql("SELECT TO_DATE(string_date, 'dd/MM/yy') FROM dfView")


My blog: https://databrickster.medium.com/

View solution in original post

Well that was certainly helpful - thank you

Jeff1
Contributor II

Based upon the initial response I went with:

my_data.frame <- my_data.frame %>%

mutate(date = to_date(data.frame_variable, "yyyy-mm-dd"))