SQL日期格式转换?[dd.mm.yy 到 YYYY-MM-DD] [英] SQL date format convert? [dd.mm.yy to YYYY-MM-DD]

查看:57
本文介绍了SQL日期格式转换?[dd.mm.yy 到 YYYY-MM-DD]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 mySQL 函数将日期从格式 dd.mm.yy 转换为 YYYY-MM-DD?

is there mySQL function to convert a date from format dd.mm.yy to YYYY-MM-DD?

例如,03.09.13 ->2013-09-03.

推荐答案

由于您的输入是 03.09.13 形式的字符串,我假设(因为今天是 2013 年 9 月 3 日)它是 dd.mm.yy.您可以使用 STR_TO_DATE:

Since your input is a string in the form 03.09.13, I'll assume (since today is September 3, 2013) that it's dd.mm.yy. You can convert it to a date using STR_TO_DATE:

STR_TO_DATE(myVal, '%d.%m.%y')

然后您可以使用 DATE_FORMAT:

Then you can format it back to a string using DATE_FORMAT:

DATE_FORMAT(STR_TO_DATE(myVal, '%d.%m.%y'), '%Y-%m-%d')

请注意,年份是 STR_TO_DATE 中的 %y(小写y")和 %Y(大写Y")代码>DATE_FORMAT.小写的版本是两位数的年份,大写的版本是四位数的年份.

Note that the year is %y (lowercase "y") in STR_TO_DATE and %Y (uppercase "Y") in DATE_FORMAT. The lowercase version is for two-digit years and the uppercase is for four-digit years.

这篇关于SQL日期格式转换?[dd.mm.yy 到 YYYY-MM-DD]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆