I am trying to convert short date format to numeric date format in SQL.
Here are the dates I am trying to convert: 16-Mar-20 18-Mar-20 12-Mar-20
I have seen many methods to re-format dates based on numeric numbers but I am working with a dataset which has varied date-types. To ensure smoothness, I wanted to know what’s the way to convert these numbers to numeric date. Note that even if I extract the day month and year individually I could multiply them with respective values to get the dates. Here’s what I have done: (Although I have tried multiple things but this is the one which has yielded me the closest result, Note that my end goal is to convert the date into numeric value)
FROM_UNIXTIME(`Start Date`, 'dd-MMM-yy') AS 'Date Numeric'
Here Start Date is formatted in the way I have mentioned above. (14-Mar-20).
Advertisement
Answer
UNIX_TIMESTAMP is what you need, but first you have do transform your data to a Date
UNIX_TIMESTAMP(STR_TO_DATE(`Start Date`, '%d-%b-%y'))
With
SELECT UNIX_TIMESTAMP(STR_TO_DATE( "14-Mar-20", "%d-%b-%y"))
You get
UNIX_TIMESTAMP(STR_TO_DATE( "14-Mar-20", "%d-%b-%y")) 1584144000