在 SQL 中对不同类型使用加号运算符时会发生什么?

发布于 2024-11-07 08:11:33 字数 120 浏览 2 评论 0原文

我想知道在不同数据类型之间使用 + 运算符时是否有定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-11-14 08:11:33

加号运算符自第一个 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.

太阳哥哥 2024-11-14 08:11:33

如果包含 char 列,则当您尝试运行查询时,如果 char 字段没有可转换为数字的内容,则会出现转换错误。如果都是数字,系统会将这些数字相加。它将把 int 转换为 float。以下是一些测试数据:

/* Appending all three types together */
SELECT (int_field + char_field + float_field) AS NewField
FROM DataTypes
/* Results: Success only when the char can be converted to a number
Conversion failed when converting the varchar value 'test      ' to data type int.
*/

/* Appending only numeric types (float and int) */
SELECT (int_field + float_field) AS NewField
FROM DataTypes
/* Results: Success */

/* Appending char and int types */
SELECT (char_field + int_field) AS NewField
FROM DataTypes
/* Results: Succes only when the char can be converted to a number
Conversion failed when converting the varchar value 'test      ' to data type int. */


/* Appending char and nvarchar types */
SELECT (char_field + nvarchar_field) AS NewField
FROM DataTypes
/* Results: Success */

请注意,在最后几个结果中,如果只有文本字段,它们将相互附加。但是,即使存在一个数字字段,系统也会尝试将它们全部转换为数字,无论顺序如何。每当使用加运算符时,即使有一个字段是数字,系统也会假定该操作是加法而不是附加操作之一。

这是在 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:

/* Appending all three types together */
SELECT (int_field + char_field + float_field) AS NewField
FROM DataTypes
/* Results: Success only when the char can be converted to a number
Conversion failed when converting the varchar value 'test      ' to data type int.
*/

/* Appending only numeric types (float and int) */
SELECT (int_field + float_field) AS NewField
FROM DataTypes
/* Results: Success */

/* Appending char and int types */
SELECT (char_field + int_field) AS NewField
FROM DataTypes
/* Results: Succes only when the char can be converted to a number
Conversion failed when converting the varchar value 'test      ' to data type int. */


/* Appending char and nvarchar types */
SELECT (char_field + nvarchar_field) AS NewField
FROM DataTypes
/* Results: Success */

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文