0

My date format looks like this '201710'. I'm trying to convert this particular string to '2017-10-01'.

How can I convert from the above mentioned format to the required format in mysql ?

I have tried using the below query:

SELECT *, DATE_FORMAT(yymm,'%Y-%m-%d') AS niceDate 
FROM table;

But the niceDate column is showing null.

Can anyone please help me out ....

1

4 Answers 4

1

I am new to SQL but i think you need to specify the column you want to format, for example im not sure on the exact code for your data but something like;

select *, DATE_FORMAT(yourdata, "%Y/%m/%d") as niceDate
from table;

Change "yourdata" to the date column you wish to format. Let me know if this helps, thanks :)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi has my solution given you any luck?
0

you can use this:

  SELECT  STR_TO_DATE(yourdatefield, '%m/%d/%Y') as 
    FROM    table

'yourdatefield' replace with your date column name.

Comments

0

you can CONCAT your string / FIELD then you have a valid DATE

SELECT str_to_date(CONCAT('201710','01'),'%Y%m%d');

SAMPLE

MariaDB [(none)]> SELECT str_to_date(CONCAT('201710','01'),'%Y%m%d');
+---------------------------------------------+
| str_to_date(CONCAT('201710','01'),'%Y%m%d') |
+---------------------------------------------+
| 2017-10-01                                  |
+---------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

Comments

0
You can use STR_TO_DATE() funcion to convert string to date, 

In your example you have not provide day part, you provide only year and month but for using this function your string should have year,month and day part Example

SELECT STR_TO_DATE('20171001','%Y%m%d') ;

4 Comments

I have tried using STR_TO_DATE() also. But, still the niceDate column is showing null
you have to provide date part also for this conversion
Actually, I'm trying to convert all the values of date field into the required format. Where I'm trying to add like this : SELECT STR_TO_DATE('date','%Y%m%d') from table; where 'date' is a column in my table.
@dev007 in that by using check date column made it complete year,month and date and then apply this function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.