How can I convert a string name into the actual month number. For instance if I have a string as 'March', how can this be converted to 3.
I know that this can be done using a case statement but then I want to know if there is any predefined function that can do this conversion.
select date_format(str_to_date('March','%M'),'%c')
firstly, using str_to_date to convert string to month name, and then format the month name to number format
month(str_to_date(MonthField,'%M'))
For more information about month and str_to_date, you can check the link
example for above is select month(str_to_date('March','%M')) from dual;
will give the result 3
Use the STRING_TO_DATE function:
SELECT STRING_TO_DATE('March','%M')
See the (function reference](https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date).
See the date formatting codes reference.
Related
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 can we check if a string is a valid date without specifying the format in MySQL.A user may enter below formats;
21-08-2013
21/08/2013
2013/08/21
21jan2018
I don't know the format in advance which a user may enter.
I tried :
Select str_to_date() but here I need to specify format which I don't want.
I just want to check if a string is valid date without specifying the format.
Please suggest.
Thanks
All four of the date strings you showed us in the question can be converted to a MySQL date using STR_TO_DATE with an appropriate format mask. So, you should be able to simply check for these date patterns, if all you want is to know if you could convert them to dates:
SELECT *
FROM yourTable
WHERE
col REGEXP '[0-9]{2}-[0-9]{2}-[0-9]{4}' OR
col REGEXP '[0-9]{2}/[0-9]{2}/[0-9]{4}' OR
col REGEXP '[0-9]{4}/[0-9]{2}/[0-9]{2}' OR
col REGEXP '[0-9]{2}[A-Z]{3}[0-9]{4}';
Does anyone here know how to parse string in mysql?
I have a column which value is in string.
example
movie1view_2014_01_01
movie2view_2014_05_02_part1
I want to get only the date that is this example 2014_01_01 and 2014_05_02 and convert it into date.
This is how you can do
select replace(SUBSTRING_INDEX(SUBSTRING_INDEX(name,'_',4),'_',-3),'_','-') as
date
from test
DEMO
How can I extract the month with a sql query in mysql.
The date format is as follows: dd/mm/yyyy.
MONTH(datecol) = $month // this does not work.
However if I change the date format to yyyy-mm-dd it works, but this is not an option.
You can parse a date in any format into an SQL date value with str_to_date. For example:
select month(str_to_date('25/07/2013', '%d/%m/%Y'));
This expression will extract the portion of the string that appears between the first and second forward slashes.
SUBSTRING_INDEX(SUBSTRING_INDEX( '25/07/2013' ,'/',2),'/',-1)
will return '07'
To get this converted to a numeric, add zero to it,
SUBSTRING_INDEX(SUBSTRING_INDEX( '25/07/2013' ,'/',2),'/',-1) + 0
will return a numeric value of 7
NOTES
This expression will work with any string value, not just strings representing a valid date in the specified format. For example:
SUBSTRING_INDEX(SUBSTRING_INDEX( 'dd/mm/yyyy' ,'/',2),'/',-1)
will return 'mm'
If only one slash is found in the string, this will return the portion following the slash. If no slashes are found in the string, this will return the entire string.
If your intent is to validate that the string represents a valid date value, in the specified format, then the STR_TO_DATE() function can be used to convert the string to a DATE. The return from that expression can be used as an argument to any of the MySQL functions that take a DATE argument, such as the MONTH() function in your sample question.
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','-','');