简体   繁体   English

MySQL中的Date_Format转换

[英]Date_Format conversion in MySQL

I need help with converting Date of birth colmun on MySQL from '21-Aug-1990' format to '21-08-1990' format such that the data still retains dateTime format and I will be able to calculate an age column.我需要帮助将 MySQL 上的出生日期列从“1990 年 8 月 21 日”格式转换为“1990 年 8 月 21 日”格式,以便数据仍保留日期时间格式,并且我将能够计算年龄列。

I have tried the following which worked.我尝试了以下有效的方法。 Now I want to calculate age from DOB.现在我想根据 DOB 计算年龄。

case
when MID(avb.BVN_DOB,4,3) LIKE '%Jan%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'01')
when MID(avb.BVN_DOB,4,3) LIKE '%Feb%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'02')
when MID(avb.BVN_DOB,4,3) LIKE '%Mar%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'03')
when MID(avb.BVN_DOB,4,3) LIKE '%Apr%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'04')
when MID(avb.BVN_DOB,4,3) LIKE '%May%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'05')
when MID(avb.BVN_DOB,4,3) LIKE '%Jun%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'06')
when MID(avb.BVN_DOB,4,3) LIKE '%Jul%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'07')
when MID(avb.BVN_DOB,4,3) LIKE '%Aug%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'08')
when MID(avb.BVN_DOB,4,3) LIKE '%Sep%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'09')
when MID(avb.BVN_DOB,4,3) LIKE '%Oct%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'10')
when MID(avb.BVN_DOB,4,3) LIKE '%Nov%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'11')
when MID(avb.BVN_DOB,4,3) LIKE '%Dec%' then REPLACE(avb.BVN_DOB, MID(avb.BVN_DOB,4,3),'12') END AS DOB 

Use STR_TO_DATE() to parse the date and DATE_FORMAT() to convert it to the desired format.使用STR_TO_DATE()解析日期并使用DATE_FORMAT()将其转换为所需格式。

DATE_FORMAT(STR_TO_DATE(avb.BVN_DB, '%d-%m-%Y'), '%d-%b-%Y') AS DOB

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

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