简体   繁体   English

对本机 function 'str_to_date' 的调用中的参数计数不正确

[英]Incorrect parameter count in the call to native function 'str_to_date'

There is a table tab1:有一个表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  |

when I used str_to_date(), I had this uncorrect result and errors.当我使用 str_to_date() 时,我得到了不正确的结果和错误。

Query:询问:

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

logs:日志:

    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]|

The question is How can I get this result:问题是我怎样才能得到这个结果:

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

MySQL version is 8.0.23 MySQL 版本为8.0.23

Seems like this should work -- check out the fiddle.看起来这应该可行——看看小提琴。

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

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

You have wrong hour specifier, should be %H in order to parse 24-hour format time.您有错误的小时说明符,应该是%H以解析 24 小时格式时间。

If you just want the year, why not use a substring operation?如果您只想要年份,为什么不使用 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 操作? After all, you're storing the value as a string:毕竟,您将值存储为字符串:

select substr(creation_date, 7, 4) as year

In the meantime, you should fix your table so it stores date/time values using the correct data type.同时,您应该修复您的表,以便它使用正确的数据类型存储日期/时间值。

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

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