Click here to Skip to main content
15,887,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have try this

SELECT DATE_FORMAT(dob,'%d%m%Y'),dob FROM abc;


not working.

What I have tried:

I have try this

SELECT DATE_FORMAT(dob,'%d%m%Y'),dob FROM abc
Posted
Updated 27-Apr-20 6:22am

Basically, don't.

Never store dates in strings - use DATETIME, DATE, or TIMESTAMP columns instead, and convert the dates to them in your presentation language (where you can cope with different user input formats).

Using strings is "easy", but it leaves you wide open for major problems later on - like date in ISO format: 2020-04-27, or US format: 04-27-2020, or just "hello" if you haven;t done any checking on your user inputs.

Change your db and store data in appropriate data types - it may seem more cumbersome, but it saves a lot of grief later on.
 
Share this answer
 
Comments
Member 11776570 27-Apr-20 12:25pm    
okay i got your point but we cant change the datatype, its client DB. So we have to convert it into datetime and stored that data into another database where it is date time datatype.
I fully agree with OriginalGriff, if possible do store the dates using appropriate data type. Not only the storing is more easy but also calculating, formatting etc is easy to do when the base type is date.

Having that said, in case you are absolutely not able to change the type you should be able to use
SQL
str_to_date(dob, '%m/%d/%Y')

For more information, see
MySQL :: MySQL 8.0 Reference Manual :: 12.6 Date and Time Functions[^]
 
Share this answer
 
Comments
Member 11776570 27-Apr-20 12:32pm    
Thanks that really works. Thanks a lot. I was just stuck into it. Already working from too many hours. Thanks buddy.
Member 11776570 28-Apr-20 8:16am    
This error is occur now

The given value of type String from the data source cannot be converted to type datetime of the specified target column.

when i use this

DATE_FORMAT(STR_TO_DATE(dob, '%m/%d/%Y'),'%Y%e%c')

to store it in datetime datatype column.
Any suggestion for it.
Wendelius 28-Apr-20 12:16pm    
Why do you convert it to string? As far as I can see you don't need DATE_FORMAT. Just try something like

INSERT INTO MyTable (....)
SELECT STR_TO_DATE(dob, '%m/%d/%Y'),
...
FROM ...
Please see solution 1 for how you should treat datetime values in the future.

Now, for your current issue, there is a mismatch between the input you specified ('03/28/1990', i.e. month/day/year) and the format string you are passing to the DATE_FORMAT function ('%d%m%Y', i.e. day/month/year).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900