MySQL date format to string - mysql

I have a dateformat but to display on front end I would like to display the data like so.
SELECT STR_TO_DATE('5,2013','%m,%Y');
The result I would like to generate is 'May 2013'.

Why are you storing dates as string values? Mysql has dedicated data types for date and time values: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
When using date, you can easily use DATE_FORMAT and set %m,%Y as formatting (second argument).

SELECT replace(date_format(str_to_date('5,2013','%m,%Y'),'%M-%Y'),'-',' ');

As to the format just read the docs: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format. You need an uppercase M for the month name. And use DATE_FORMAT to get from a date to a string.
SELECT DATE_FORMAT(DATE '2013-05-01', '%M %Y');

Assuming that you have 5,2013 stored in your database, you need to use DATE_FORMAT after parsing the string:
SELECT DATE_FORMAT(STR_TO_DATE('5,2013','%m,%Y'), '%b %Y')
Why? Because it seems you don't have a date type stored in the database. So you parse it to date using STR_TO_DATE. This give you the default date format. DATE_FORMAT let you apply other formattings on it.
All avaliable formatting values are documented here: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
In general, I would recommend to think about storing date objects instead of custom date strings like 5,2013. This avoids those castings, because you could directly use DATE_FORMAT. It also has benefits on filtering/ordering and performance. Using date types, you could easily sort by it or filter e.g. everything in month X.
I don't say its not possible with custom strings like 5,2013 but would be more complex and could result in bad performance if the db grows.

You can use the functions:
str_to_date() to convert the string (after concatenating the prefix '1' to your string) to a valid date and then
date_format() to reformat the date:
SELECT date_format(str_to_date(concat('1,', ?),'%d,%m,%Y'), '%b %Y');
Replace ? with your string.
See the demo.
Result:
May 2013

Related

How to convert '10-Mar-1993' to date while inserting records in mysql?

I have written the following query:
INSERT INTO programs
(programId,programName,startDate,annualGoal)
VALUES
(135,'Community Evergreening',STR_TO_DATE('10-Mar-2013','dd-mmm-yyyy'),25000);
But, it says "Error Code: 1411. Incorrect datetime value: '10-Mar-2013' for function str_to_date".
I am not able to understand what I am doing wrong here.
You'll need to represent the date format like this: %d-%b-$Y
STR_TO_DATE('10-Mar-2013', '%d-%b-%Y')
The problem is that you are using the format dd-mmm-yyyy. However, MySQL's date formatting does not use that format.
If you look at the documentation for str_to_date, it says that it is the inverse of date_format, and that you can look at the documentation for date_format for a table of the characters that you can use to represent a date format.
For the specifiers that can be used in format, see the DATE_FORMAT() function description.
Try STR_TO_DATE('10-Mar-2013', '%d %b %Y')
Got from here

MySQL String to Date converting with varying strings?

I need to convert dates of varying strings. They come in 3 different ways.
yyyy/mm/dd
mm/dd/yyyy
or blank (can fill in some default)
What is a good way to handle this situation for an INSERT statement?
Use STR_TO_DATE function in combination with COALESCE - something like:
set #strdate := '2014/05/10';
select COALESCE(STR_TO_DATE(#strdate,'%m/%d/%Y'),STR_TO_DATE(#strdate,"%Y/%m/%d"))
You can use MySQL str_to_date() function. It requires one format and will return NULL if the data doesnt match the format. UseCoalesce with str_to_date with str_to_date in descending order of likelihood of the format.
Example if yyyy/mm/dd is more common than mm/dd/yyyy then use
COALESCE(STR_TO_DATE(your_date_here,'%Y/%m/%d'),STR_TO_DATE(#strdate,"%m/%d/%Y"))
Edit: This is the same as Ondřej Šotek's answer but with a possible performance improvement

Why i got null from this query

Following is my sql query kindly let me know why is it returning null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
From MySQL STR_TO_DATE function:
The server scans str attempting to match format to it. ... Scanning
starts at the beginning of str and fails if format is found not to
match.
This is why your first query fails: 11-APR-74 does not look like %e%b%Y, so date cannot be parsed. You should do instead
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
From MySQL Date and Time types:
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
This is why your second query fails: 74 is not a valid day of month, you should do instead
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
Note, that DATE_FORMAT is usually used on DB values, not string literals as you do - to get an output different from the default one.
If you want to convert from string to date
Select STR_TO_DATE ('11-APR-74','%d-%b-%y')
use it like ::
Select STR_TO_DATE ('11-APR-74','%e-%b-%Y')
Because '%e%b%Y' format does not correspond to '11-APR-74' string value (as STR_TO_DATE function expects), and because '11-APR-74' value is of type CHAR, but not DATETIME (as DATE_FORMAT function expects).
If you want to reformat a date represented by a CHAR value, convert it from its original format to DATETIME first, and then convert it to a string of desired format:
SELECT DATE_FORMAT(STR_TO_DATE('11-APR-74','%e-%b-%Y'),'%e%b%Y');
By the way, you could strip dashes with a plain string sunction:
SELECT REPLACE('11-APR-74','-','');

Converting date format using str_to_date

In one of my Mysql database table the dates are stored in the format 31-Jan-05.
I'm trying to convert this format to 2005-01-31 before inserting them in other tables.
I've tried in this way str_to_date(exam_date, '%d%M%Y'), but i encounter the following error
Incorrect datetime value: '31-Jan-05' for function str_to_time
Can't i change the date format from 31-Jan-05 to 2005-01-31 using str_to_date?
Thanks in advance.
Yes. But you have two problems.
The second parameter is the current date format. (i.e. of the string)
You need to have the proper format (i.e. %b instead of %M).
Read the docs the for str_to_date().
str_to_date(exam_date, '%d-%b-%y')
Note: If you don't have a zero padded day, then you need to use %e instead of %d.

Convert datetime value into string

I am fetching the current date & time using NOW() in mysql. I want to convert the date value into a varchar and concat it with another string. How do I do it?
Use DATE_FORMAT()
SELECT
DATE_FORMAT(NOW(), '%d %m %Y') AS your_date;
This is super old, but I figured I'd add my 2c. DATE_FORMAT does indeed return a string, but I was looking for the CAST function, in the situation that I already had a datetime string in the database and needed to pattern match against it:
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
In this case, you'd use:
CAST(date_value AS char)
This answers a slightly different question, but the question title seems ambiguous enough that this might help someone searching.
Try this:
concat(left(datefield,10),left(timefield,8))
10 char on date field based on full date yyyy-MM-dd.
8 char on time field based on full time hh:mm:ss.
It depends on the format you want it. normally you can use script above and you can concat another field or string as you want it.
Because actually date and time field tread as string if you read it. But of course you will got error while update or insert it.