意外的查询结果

编程入门 行业动态 更新时间:2024-10-22 11:23:52
本文介绍了意外的查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么我从 sql-server 得到以下结果?

Why I get following results from sql-server?

SELECT '' + 12 C1, CONVERT(int, '') C2, CASE WHEN '' = ' ' THEN 'equal' ELSE 'not equal' END C3

Sql-Server Fiddle 演示

--Results | C1 | C2 | C3 | ------------------- | 12 | 0 | equal |

C3 已得到答复.每个人都回答认为 '' + 12 = 12 是字符串连接,但它是一种数学运算.目前尚不清楚为什么(NOT 如何)'' 在 sql-server 中转换为 0.

C3 has been answered. Everyone answered thinking '' + 12 = 12 is string concatenation but it is a mathematical operation. It is not clear why (NOT how) '' is converting to a 0 in sql-server.

推荐答案

SQL Server 有一个奇怪的特性,如果比较两个字符串,并且它们的长度不相等,那么它会将较短的一个填充到较长的一个,然后比较它们.填充字符是一个空格.support.microsoft/kb/316626

SQL Server has a strange feature whereby, if two strings are compared, and they are not of equal length, then it will pad the shorter one to the length of the longer one and then compare them. The padding character is a space. support.microsoft/kb/316626

C1 &C2 只是隐含的 &将空字符串显式转换为 0.

C1 & C2 are just implicit & explicit conversion of an empty string to 0.

更多推荐

意外的查询结果

本文发布于:2023-10-25 22:06:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:查询结果   意外

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!