MYSQL:如何将字符串转换成一个月(数字) [英] MYSQL: How to convert a string into a month (Number)
问题描述
我有几个月的简短版本:JAN, FEB, MAR
,等等.
I have the short version of months: JAN, FEB, MAR
, etc.
,并希望将它们转换为各自的数值:1, 2, 3
等
and would like to convert them to it's respective numeric value: 1, 2, 3
, etc
此外,我希望能够在数值月份值之间来回切换,以短"月份名称(JAN, FEB, MAR
)和长版本名称(January, February, March
等)
Also, I would like to be able to change back and forth between the numeric-month-value, to a "short" month name (JAN, FEB, MAR
) and it's long version (January, February, March
, etc)
注意:正如@ dipu-raj所指出的,这不是重复的,因为我要问与
NOTE: As @dipu-raj pointed out, this is not a duplicate because I am asking the opposite to MySQL MONTHNAME() from numbers and the answer IS different as well BECAUSE it requires different functions
推荐答案
要将缩写转换为全月名称,请使用:
To convert abbrevation to full month name use:
mysql> select monthname(str_to_date('Mar','%b'));
+------------------------------------+
| monthname(str_to_date('Mar','%b')) |
+------------------------------------+
| March |
+------------------------------------+
要将缩写转换为数字,请使用:
To convert abbrevation to number use:
mysql> select month(str_to_date('Mar','%b'));
+--------------------------------+
| month(str_to_date('Mar','%b')) |
+--------------------------------+
| 3 |
+--------------------------------+
这篇关于MYSQL:如何将字符串转换成一个月(数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!