简体   繁体   English

从字符串中提取月份名称和年份

[英]Extracting Monthname and Year from a string

I have to extract the month name and year from the string of the form mmm-yy (Eg. Jan-20) using mysql query.我必须使用 mysql 查询从 mmm-yy(例如 Jan-20)形式的字符串中提取月份名称和年份。 I have tried the following query it returns only NULL.我尝试了以下查询,它只返回 NULL。 The query is:查询是:

SELECT monthname(mon)FROM month_extract_plan

Sample data: 

Jan-20
Feb-20
Dec-19

You can use a Query like this:您可以使用这样的查询:

SELECT 
    SUBSTRING_INDEX(str, '-', 1)  as MyMonth,
    SUBSTRING_INDEX(str, '-', -1) as MyYear
FROM month_extract_plan;

Use STR_TO:Date使用 STR_TO:日期

SELECT monthname(STR_TO_DATE('Jan-20','%b-%y')) ,YEAR(STR_TO_DATE('Jan-20','%b-%y')) 

gets you得到你

'January 2020 '2020 年 1 月

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM