调用本机函数时参数计数不正确 'str_to_date'

Incorrect parameter count in the call to native function 'str_to_date'

有一个table tab1:

|creation_date      | acc_num             | status|
|-------------------|---------------------|-------|
|31.03.2021 07:43:43| 11111111111111111111| deny  |
|31.03.2021 07:43:43| 11111111111111111111| deny  |
|31.03.2021 01:43:20| 22222222222222222222| admit |
|30.03.2020 21:13:21| 33333333333333333333| deny  |
|30.03.2021 20:28:19| 22222222222222222222| deny  |
|30.03.2021 20:28:19| 44444444444444444444| deny  |

当我使用 str_to_date() 时,我得到了这个不正确的结果和错误。

查询:

    select year(str_to_date(creation_date, "%d.%m.%Y %h:%i:%s")) as year
    from tab1;

日志:

    Incorrect parameter count in the call to native function 'str_to_date'
    Unknown column 'creation_date' in 'field list'
    Incorrect datetime value: '30.03.2020 21:13:21' for function str_to_date
    ...
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
    from tab1' at line 1
    Incorrect datetime value: '30.03.2020 21:13:21' for function str_to_date
    ...
    Incorrect datetime value: '30.03.2021 20:28:19' for function str_to_date
result:
|year|
|----|
|2021|
|2021|
|2021|
|[NULL]|
|[NULL]|
|[NULL]|

问题是我怎样才能得到这个结果:

|year|
|----|
|2021|
|2021|
|2021|
|2020|
|2021|
|2021|

MySQL 版本为 8.0.23

这似乎应该有效——查看 fiddle。

select year(STR_TO_DATE(c,'%d.%m.%Y'))
from tab;

http://sqlfiddle.com/#!9/8cfa2a5/3

您的小时说明符有误,应 %H 以便解析 24 小时格式时间。

如果只想要年份,为什么不使用子字符串操作呢?毕竟,您将值存储为字符串:

select substr(creation_date, 7, 4) as year

同时,您应该修复 table,以便它使用正确的数据类型存储 date/time 值。