mysql date_format unable to parse 20-may-2013 - mysql

using date_format as specified in the doc from mysql
select date_format('20-May-2013', '%d-%b-%Y');
returns null, why?

You are trying to parse a date using DATE_TIME(), but that function is meant to do the inverse - take a known formatted date and display it using a custom format. What you probably want is the STR_TO_DATE() function:
select str_to_date('20-May-2013', '%d-%b-%Y');

Related

MySQL date format to string

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

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

Trouble Converting varchar to date

I'm using MAMP's phpMyAdmin as my database.
I'm having troubles using the CONVERT sql function. Is this a common problem?
SELECT CONVERT(varchar(10), Date, 103)
This will give me the following syntax error. Which is one of the usual errors given but I've been trying a lot of different ways.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'varchar(10), Date, 103)' at line 1
If the above can't be solved can I bypass this issue by adding a new column with a specific date format style?
You are trying to use sql-server syntax in mysql database.
mysql does not have 3rd parameter for CONVERT function.
CONVERT(expr,type), CONVERT(expr USING transcoding_name)
The CONVERT() and CAST() functions take an expression of any type and
produce a result value of a specified type.
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert
to do the get same format in mysql you need to use DATE_FORMAT() function
this should give you the same resultDATE_FORMAT(date,'%d/%m/%Y')
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select DATE_FORMAT(Current_Date(),'%d/%m/%Y')
here is SQL Fiddle
http://sqlfiddle.com/#!2/d41d8/36014
The date is already in the correct format but it is stored as varchar(10) Current looks like: 21/04/2014
Yes, I have a column that has dates but is set to varchar as it wasn't being used now I need to convert these into dates.
If you are trying to get a date form from a string form of date, then
select str_to_date( date_column, '%d/%m/%Y' ) as date from table_name
This will return 21/04/2014 as 2014-04-21, regular default date format in MySQL.
It depends on what you want to do.
If you're trying to convert an integer that represents a Unix timestamp to a date with MySQL, you may want to use FROM_UNIXTIME()
See also: MySQL: Convert INT to DATETIME
If you're trying to convert a string that represents a date into a date, you can use STR_TO_DATE()
After that, if you perform some processing on the date and want to convert it back to a string, you'll need DATE_FORMAT()
See also: mySQL convert varchar to date

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.