本文介绍了MySQL将varchar转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将varchar值1/9/2011转换为mySQL中的日期,并且我只需要月份和年份.这样我便可以使用PERIOD_DIFF
函数(因此,我需要将上述内容转换为201101).
I need to convert a varchar value of 1/9/2011 to a date in mySQL and I want only the month and year. So that I can then use the PERIOD_DIFF
function (so I would need the above to be converted to 201101).
我已经尝试过使用STR_TO_DATE
函数的各种方式:
I've tried various ways using the STR_TO_DATE
function:
SELECT STR_TO_DATE(CYOApp_oilChangedDate, '%m/%Y') FROM CYO_AppInfo
但是我得到的结果很奇怪...(例如:2009-01-00)
But I get weird results... (for example: 2009-01-00)
我做错了什么?
推荐答案
select date_format(str_to_date('31/12/2010', '%d/%m/%Y'), '%Y%m');
或
select date_format(str_to_date('12/31/2011', '%m/%d/%Y'), '%Y%m');
很难从您的例子中分辨出来
hard to tell from your example
这篇关于MySQL将varchar转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!