CAST function should support INT synonym for SIGNED. i.e. CAST(y AS INT)
Bug #73054 | CAST function should support INT synonym for SIGNED. i.e. CAST(y AS INT) | ||
---|---|---|---|
Submitted: | 19 Jun 2014 15:55 | Modified: | 30 Jun 2014 11:12 |
Reporter: | Morgan Tocker | Email Updates: | |
Status: | Verified | Impact on me: | None |
Category: | MySQL Server: DML | Severity: | S4 (Feature request) |
Version: | OS: | Any | |
Assigned to: | |||
Triage: | Needs Triage: D5 (Feature request) |
Description:
Reported via twitter:
https://twitter.com/lukaseder/status/479288364106280960 Other databases will support CAST(y AS INT), but MySQL requires CAST(y AS SIGNED). I tried reading the SQL-92 standard (not the easiest text to follow), and it doesn't seem to indicate it must be SIGNED. Is it possible to support INT as an alias for interoperability? How to repeat:
mysql [localhost] {msandbox} (test) > select version();
+-----------+
| version() |
+-----------+
| 5.7.4-m14 |
+-----------+
1 row in set (0.00 sec) mysql [localhost] {msandbox} (test) > SELECT CAST('5' as INT);
ERROR 1064 (42000): 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 'INT)' at line 1
mysql [localhost] {msandbox} (test) > SELECT CAST('5' as SIGNED);
+---------------------+
| CAST('5' as SIGNED) |
+---------------------+
| 5 |
+---------------------+
1 row in set (0.00 sec) Suggested fix:
mysql [localhost] {msandbox} (test) > SELECT CAST('5' as INT);
+---------------------+
| CAST('5' as INT) |
+---------------------+
| 5 |
+---------------------+
1 row in set (0.01 sec)
Thanks for reporting this, Morgan. Some additional notes on my motivation (in the original Tweet). All other SQL dialects support the same type notation for CAST expressions as for column specifications in DDL. Concretely (citing parts from SQL-92): General definition of <data type>
--------------------------------- <data type> ::=
<character string type> [ CHARACTER SET <character set specification> ]
| <national character string type>
| <bit string type>
| <numeric type>
| <datetime type>
| <interval type> <character string type> ::=
CHARACTER [ <left paren> <length> <right paren> ]
| CHAR [ <left paren> <length> <right paren> ]
| CHARACTER VARYING <left paren> <length> <right paren> <data type> can be used in <cast specification>...
-------------------------------------------------- <cast specification> ::=
CAST <left paren> <cast operand> AS <cast target> <right paren> <cast target> ::=
<domain name>
| <data type> ... and also in <table definitions>
----------------------------------- <table definition> ::=
CREATE [ { GLOBAL | LOCAL } TEMPORARY ] TABLE <table name>
<table element list> <table element list> ::=
<left paren> <table element> [ { <comma> <table element> }... ] <right paren> <table element> ::=
<column definition>
| <table constraint definition> <column definition> ::=
<column name> { <data type> | <domain name> } MySQL has its own way of dealing with data types when used in a <cast specification>. Apart from possibly historic reasons, it is unclear why there are two sets of data types that cannot be used interchangeably. Obviously, this is not limited to INT types, but to all supported data types, which should be supported as <cast targets>.
Hello Morgan, Thank you for the feature request! // Oracle Database 11g SQL> SELECT CAST ('5' AS INT) from dual; CAST('5'ASINT)
--------------
5 SQL> SELECT * FROM V$VERSION; BANNER
-------------------------------------------------------------------------------- Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for 32-bit Windows: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production SQL> // MySQL 5.7 mysql> SELECT CAST('5' as INT);
ERROR 1064 (42000): 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 'INT)' at line 1
mysql>
mysql> SELECT CAST('5' as SIGNED);
+---------------------+
| CAST('5' as SIGNED) |
+---------------------+
| 5 |
+---------------------+
1 row in set (0.00 sec) mysql> Thanks,
Umesh
I think we miss some other things here.
MySQL has several sizes of int apart from them being signed or unsigned. So the CAST() function should really be able to convert to these specific types as the resultant value may later be used in a comparison. So probably we should have CAST( XXXX AS [unsigned] (big|medium|tiny|)int ) to be clearer here and to catch out of value cast conversions which probably at least in strict mode should generate an error. I also miss the possibility of CASTing into a timestamp(X) value as these days we may find that useful. The reason for the CAST() as far as I can see is to force the resultant type to be something very specific so that anywhere the value is later used there's no doubt what type and size it has.
CAST function should support INT synonym for SIGNED. i.e. CAST(y AS INT)的更多相关文章
- [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示
有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...
- flultter listview异常type '(BuildContext, int) => dynamic' is not a subtype of type '(BuildContext, int) => Widget'
type '(BuildContext, int) => dynamic' is not a subtype of type '(BuildContext, int) => Widget' ...
- SQL技术内幕-7 varchar类型的数字和 int 类型的数字的比较+cast的适用
DECLARE @x VARCHAR(10); DECLARE @y INT; DECLARE @z VARCHAR(10); SET @x = '1000'; SET @y = '2000'; SE ...
- MySQL数据类型 int(M) 表示什么意思?详解mysql int类型的长度值问题
MySQL 数据类型中的 integer types 有点奇怪.你可能会见到诸如:int(3).int(4).int(8) 之类的 int 数据类型.刚接触 MySQL 的时候,我还以为 int(3) ...
- Java中返回值定义为int类型的 方法return 1返回的是int还是Integer&&finally中return问题
在Java中返回值定义为int类型的 方法return 1:中返回的是Integer值,在返回的时候基本类型值1被封装为Integer类型. 定义一个Test类,在异常处理try中和finally中分 ...
- path('<int:question_id>/vote/', views.vote, name='vote')中的<int:question_id>的含义
path('<int:question_id>/vote/', views.vote, name='vote')<int:question_id>用于匹配URL的值,并将扑捉到 ...
- 为什么声明了int型的变量并且直接初始化后,int型变量的地址一直在变化?
/************************************************************************* > File Name: ptr_varia ...
- java中int和Integer的区别?为什么有了int还要有设计Integer?
参考https://blog.csdn.net/chenliguan/article/details/53888018 https://blog.csdn.net/myme95/article/det ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
随机推荐
- ASP.NET MVC 路由(一)
ASP.NET MVC路由(一) 前言 从这一章开始,我们即将进入MVC的世界,在学习MVC的过程中在网上搜索了一下,资料还是蛮多的,只不过对于我这样的初学者来看还是有点难度,自己就想看到有一篇引导性 ...
- Node.js Base64 Encoding和Decoding
如何在Node.js中encode一个字符串呢?是否也像在PHP中使用base64_encode()一样简单? 在Node.js中有许多encoding字符串的方法,而不用像在JavaScript中那 ...
- Objective-C 工厂模式(下) -- 抽象工厂模式
相比简单工厂模式, 只有一个工厂 能生产的手机也是固定的 抽象工厂模式类似于有很多家工厂, 当用户要买什么手机就创建对应的工厂去生产 比如用户要买iPhone就创建一个Apple工厂来生产手机, 要买 ...
- 2013 duilib入门简明教程 -- 结合win32和MFC (16)
虽然duilib自带在MFC中使用duilib的Demo,但只是MFC窗口和duilib窗口不重叠的情况.如果要在MFC窗口中嵌入duilib控件,或者在duilib控件中嵌入MFC的控件的话 ...
- Atitit 数据库的事件机制--触发器与定时任务attilax总结
Atitit 数据库的事件机制--触发器与定时任务attilax总结 1.1. 事件机制的图谱1 2. 触发器的类型2 3. 实现原理 After触发器 Vs Instead Of触发器2 3.1. ...
- C#设计模式-原型模式
在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,如果我们用new操作符去创建这样的类实例,这未免会增加创建类的复杂度和耗费更多的内存空间,因为这样在内存中分配 ...
- OO设计原则
开闭原则(OCP) 里氏代换原则(LSP) 依赖倒转原则(DIP) 接口隔离原则(ISP) 合成/聚合利用原则(CARP) 迪米特法则(LOD)
- HTML5_02之视频、音频、Canvas
1.HTML5新特性之视频播放--video: ①例:<video src=""></video> ②video标签默认为300*150的inline-bl ...
- 面向对象设计之SRP(单一职责)原则
SRP设计原则面向对象类设计的第一个原则,最优先考虑的因素 一个类应该有且仅有一个职责.所谓一个类的职责是指引起该类变化的原因,如果一个类具有一个以上的职责,那么就会有多个不同的原因 引起该类变化,其 ...
- nodejs 批量修改、删除
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAAQhCAIAAABDaAVHAAAgAElEQVR4nOydeXxU5aH359733vd9b/ ...