

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to convert a string into a month (Number)?
Use the str_to_date() method −
select month(str_to_date(yourColumnName,'%b')) from yourTableName;
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, MonthName varchar(100) ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(MonthName) values('Jan'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(MonthName) values('Mar'); Query OK, 1 row affected (0.13 sec) Display all records from the table using select statement: mysql> select *from DemoTable;
Output
+----+-----------+ | Id | MonthName | +----+-----------+ | 1 | Jan | | 2 | Mar | +----+-----------+ 2 rows in set (0.00 sec)
Following is the query to convert a string into a month (Number) −
mysql> select month(str_to_date(MonthName,'%b')) from DemoTable;
Output
+------------------------------------+ | month(str_to_date(MonthName,'%b')) | +------------------------------------+ | 1 | | 3 | +------------------------------------+ 2 rows in set (0.05 sec)
- Related Questions & Answers
- MySQL query to convert timestamp to month?
- How to convert a string into number in PHP?
- MySQL query to convert a string like “1h 15 min” into 75 minutes?
- How to concatenate MySQL distinct query results into a string?
- MySQL query to convert a single digit number to two-digit
- MongoDB query to convert numeric string to number
- MySQL query to select records from a table on the basis of a particular month number?
- How to convert year, month, and day of the month into a complete date in R?
- MySQL query to select all entries from a particular month
- Convert DateTime Value into String in MySQL?
- How to convert a JSON string into a JavaScript object?
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- C Program to convert a number to a string
- Convert string to a number in Java
- Convert hex string to number in MySQL?
Advertisements