在 SQL 中对不同类型使用加号运算符时会发生什么?
我想知道在不同数据类型之间使用 + 运算符时是否有定义的 SQL 标准响应。
即SELECT (int_col + char_col + float_col) AS mix_value
I was wondering if there is a defined SQL standard response when using the + operator among different data types.
i.e. SELECT (int_col + char_col + float_col) AS mixed_value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
加号运算符自第一个 SQL 标准 ANSI SQL-86 起就已定义。除其他外,它定义了数字、间隔以及日期和间隔之间的加法。没有处理带有字符数据的加号运算符的标准方法。
The plus sign operator has been defined since ANSI SQL-86, the first SQL standard. It defines, among other things, addition between numbers, intervals, and dates and intervals. There is no standard method of handling a plus sign operator with character data.
如果包含 char 列,则当您尝试运行查询时,如果 char 字段没有可转换为数字的内容,则会出现转换错误。如果都是数字,系统会将这些数字相加。它将把 int 转换为 float。以下是一些测试数据:
请注意,在最后几个结果中,如果只有文本字段,它们将相互附加。但是,即使存在一个数字字段,系统也会尝试将它们全部转换为数字,无论顺序如何。每当使用加运算符时,即使有一个字段是数字,系统也会假定该操作是加法而不是附加操作之一。
这是在 SQL Server 2008 上测试的。
If you are including a char column, you will get a conversion error when you try to run the query if the char field does not have something that can be converted to a number. The system will add the numbers together if they are all numbers. It will convert the int to a float. Here is some test data:
Notice in the last couple results that if there are only text fields, they will append to each other. However, if even one numeric field is present, the system tries to convert them all to numeric, regardless of order. Whenever the plus operator is used, if even one field is numeric the system assumes the operation is one of addition not appending.
This was tested on SQL Server 2008.