当前位置:   article > 正文

mysql使用cast()/convert()语法正确报错_mysql cast报错 '(' excepted

mysql cast报错 '(' excepted

1.mysql使用cast()/convert()函数语法正确但是报语法错误

2. 错误案例:

2.1使用场景:

  • 简单查询表中数据,其中对其中部分属性进行格式转化,例如限制其字段内容的显示长度。
# 查询文章指定字符长度
-- note_text数据类型为text
SELECT note_id, CONVERT(note_text, VARCHAR(10)) as note_text_desc
FROM productnotes;

/*错误结果:1064 - 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 'VARCHAR(10)) as note_text_desc
 FROM productnotes' at line 1*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.2错误原因:

转化格式仅仅支持 BINARY、CHAR、DATE、DATETIME、TIME、DECIMAL、SIGNED、UNSIGNED 数据类型。

2.3修改:

使用char型代替可实现该功能,如下图例子

SELECT note_id, CONVERT(note_text, CHAR(10)) as note_text_desc
FROM productnotes;
/* result:
101	Customer c
102	Can shippe
103	Safe is co
104	Quantity v
105	Included f
106	Matches no
107	Please not
108	Multiple c
109	Item is ex
110	Customer c
111	Shipped un
112	Customer c
113	Customer c
114	Call from 
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.扩展应用

如需具体了解mysql的cast()和convert()使用请参见MySQL 8 中的数据类型转换

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/840271?site
推荐阅读
相关标签
  

闽ICP备14008679号