简体   繁体   English

将 SQL 中的 dd.mm.yyyy 转换为 yyyy-mm-dd

[英]Convert dd.mm.yyyy to yyyy-mm-dd in SQL

I need to convert a date in format dd.mm.yyyy to yyyy-mm-dd For example, if the date is 04.12.2020, i want it to be like 2020-12.04(which is 4th december) But the command giving me the output as 2020-04-12( which is 12th April) select convert(date,'04.12.2020' )我需要将 dd.mm.yyyy 格式的日期转换为 yyyy-mm-dd 例如,如果日期是 04.12.2020,我希望它像 2020-12.04(即 12 月 4 日)但是给我的命令output 为 2020-04-12(即 4 月 12 日) select 转换(日期,'04.12.2020')

I think you just want str_to_date() :我想你只想要str_to_date()

select str_to_date('04.12.2020', '%d.%m.%Y')

It sounds like you have a date as a string?听起来你有一个日期作为字符串? (ie. the "04.12.2020"). (即“04.12.2020”)。 MySql has a STR_TO_DATE() function: MySql 有一个 STR_TO_DATE() function:

select str_to_date('04.12.2020', '%d.%m.%Y')

That should convert it to an actual date datatype.那应该将其转换为实际的日期数据类型。

If needed an actual date can then be formated in which ever way you want or just visualized depending on what you're trying to do with DATE_FORMAT():如果需要,可以按照您想要的任何方式格式化实际日期,或者根据您尝试使用 DATE_FORMAT() 执行的操作将其可视化:

select date_format(date, '%Y-%m-%d')

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

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