[英]T-SQL string concatenation `'string' + str(integer)` introduces extra space character
Sorry for the somewhat trivial question, but why does 对不起有点微不足道的问题,但为什么呢
select ('https://stackoverflow.com/users/' + str(Id)) as Link
from Users
where DisplayName = 'Jon Skeet';
when entered into the Data Explorer return 当进入Data Explorer返回时
https://stackoverflow.com/users/ 22656
instead of 代替
https://stackoverflow.com/users/22656
? ?
According to Microsoft's documentation on T-SQL's +
operator , 根据微软关于T-SQL +
运算符的文档 ,
'book' + 'case'
should give 应该给
'bookcase'
not 不
'book case'
and according to the Data Explorer documentation , the SQL-flavor used in the Stack Exchange Data Explorer is indeed T-SQL. 根据Data Explorer文档 ,Stack Exchange Data Explorer中使用的SQL风格确实是T-SQL。
Some additional experiments: 一些额外的实验:
select str(42);
returns "42"
, without extra spaces (see EDIT below) . 返回"42"
,没有额外的空格(参见下面的编辑) 。
select ('foo' + 'bar');
returns "foobar"
, also without spaces. 返回"foobar"
,也没有空格。
select ('foo' + '42');
returns "foo42"
, so it doesn't treat digits specially or anything like that. 返回"foo42"
,因此它不会特别处理数字或类似的数字。
To me, it looks as if basic semantic compositionality principle is violated. 对我来说,看起来好像违反了基本的语义成分原则。 What am I missing here? 我在这里错过了什么?
EDIT The problem turned out to be the wrong assumption that 编辑问题证明是错误的假设
select str(42);
returns "42"
. 返回"42"
。 It actually returns 它实际上回来了
" 42"
but the spaces are ignored by the browser-based GUI. 但是基于浏览器的GUI会忽略这些空格。
Here is another example that demonstrates the problem more clearly: 这是另一个更清楚地说明问题的例子:
select 'foo' + str('42');
seems to return 似乎回归了
"foo 42"
but it actually returns 但它实际上会返回
"foo 42"
as can be seen in this query: 在此查询中可以看到:
select LEN('foo' + str('42'));
which returns 13 (not 5 and also not 6). 返回13(不是5,也不是6)。 Many thanks @lad2025 for pointing this out. 非常感谢@ lad2025指出这一点。
So, it was mostly an "optical illusion" caused by a somewhat inaccurate representation of string-results in the browser. 因此,它主要是由于浏览器中字符串结果的某种不准确表示而导致的“视错觉”。
Returns character data converted from numeric data. 返回从数字数据转换的字符数据。
STR ( float_expression [ , length [ , decimal ] ] ) STR(float_expression [, length [,decimal]])
Is the total length. 是总长度。 This includes decimal point, sign, digits, and spaces. 这包括小数点,符号,数字和空格。 The default is 10. 默认值为10。
select REPLACE(str(Id), ' ', '-')
from Users
where DisplayName = 'Jon Skeet';
OUTPUT:
-----22656
I would simply use CONCAT
: 我只想使用CONCAT
:
select CONCAT('https://stackoverflow.com/users/', id) AS link
from Users
where DisplayName = 'Jon Skeet';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.