Format Datetime in MySql (YYYYMMDD) Format

6765
Views
2
Answers

How do I format the date in MYSql in YYYMMDD format?

I have been trying few queries

Select date_format('2012-01-01','%yyyy%mm%dd')

The output is 12yyy01m01d

Select date_format('2012-01-01','%y%m%d')

The output is 120101

I need that to be in the format 20120101 Any Suggestions?

edit | flag
Mad Maddy
Asked on: Feb 07, 2012 at 9:16PM

2 Answers

124
1
124

You should be using %Y instead of %y.

%Y (caps) will give you year in the format YYYY.
%y (lowercase) will return year in YY format

Select date_format('2012-01-01','%Y%m%d') as date

Output:
20120101

edit | flag
Gokul A
Answered on: Feb 07, 2012 at 9:19PM
153
0
153

Hi Maddy.
the below query should help you through

SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]
edit | flag
Ehtesham Siddiqui
Answered on: Sep 06, 2012 at 4:37PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags