8

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

https://stackoverflow.com/users/ 22656

instead of

https://stackoverflow.com/users/22656

?

According to Microsoft's documentation on T-SQL's + operator,

'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.


Some additional experiments:

select str(42);

returns "42", without extra spaces (see EDIT below).

select ('foo' + 'bar');

returns "foobar", also without spaces.

select ('foo' + '42');

returns "foo42", so it doesn't treat digits specially or anything like that.


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". It actually returns

"        42"

but the spaces are ignored by the browser-based 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). Many thanks @lad2025 for pointing this out.

So, it was mostly an "optical illusion" caused by a somewhat inaccurate representation of string-results in the browser.

8
  • I'm going to guess ID isn't numeric. What datatype is ID? varchar? if so ID contains the extra space. Commented May 24, 2018 at 13:29
  • The documnentation for str says: "A short float_expression is right-justified in the specified length, and a long float_expression is truncated to the specified number of decimal places. For example, STR(12,10) yields the result of 12. This is right-justified in the result set." Commented May 24, 2018 at 13:30
  • @xQbert It says int. Commented May 24, 2018 at 13:30
  • 1
    The issue is with STR function Commented May 24, 2018 at 13:30
  • 1
    (Spaces invisible in comment inline-code, so replaced spaces by -) Ok, so, the select str(42); returns "---------42" instead of "42", but the spaces are ignored by the browser-based 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). Many thanks @lad2025 for pointing this out. Commented May 24, 2018 at 13:44

1 Answer 1

9

The problem is STR:

Returns character data converted from numeric data.

STR ( float_expression [ , length [ , decimal ] ] )

Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.

select REPLACE(str(Id), ' ', '-')
from Users
where DisplayName = 'Jon Skeet';

OUTPUT:
-----22656

I would simply use CONCAT:

select CONCAT('https://stackoverflow.com/users/', id) AS link
from Users
where DisplayName = 'Jon Skeet';

See Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Is then the "42" output for str(42) merely an artifact of the browser-based GUI that ignores whitespace?
concat applies implicit conversion of the int datatype to string then right?
@xQbert Yes, the is implicit cast
@AndreyTyukin SELECT LEN(str(42)) -- 10 Demo
Yes, and also select LEN('foo' + str('42')); returns 13, not 5 or 6. It's just the browser-based GUI that hides the extra spaces. Thank you, that explains it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.