Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date YYYYMMDD to YY-MM-DD in MySQL query

Tags:

mysql

I have a date column which is in YYYYMMDD or 20120101 format. This is because SAP stores it in this format, so cannot change this.

How can I convert this to a YYYY-MM-DD format in a MySQL query? In DB2, I used a to_date() function.

In MySQL, I'm trying the STR_TO_STRING() function but it returns 'null'.

  SELECT STR_TO_DATE(VBAP.ERDAT,'%Y-%m-%d') FROM VBAP
like image 376
kouton Avatar asked Jul 07 '12 11:07

kouton


People also ask

How do you convert date format from Yyyymmdd to yyyy-mm-dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].

How do I change date format from YYYY-MM-DD in MySQL?

MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.

How do I convert a date from one format to another in MySQL?

Use STR_TO_DATE() method from MySQL to convert. The syntax is as follows wherein we are using format specifiers. The format specifiers begin with %. SELECT STR_TO_DATE(yourDateColumnName,'%d.

What is date format in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' .


1 Answers

This works..

date_format(str_to_date(VBAP.ERDAT, '%Y%m%d'),'%Y-%m-%d')
like image 90
kouton Avatar answered Sep 28 '22 05:09

kouton