October 8, 2009 at 2:29 am
Good question. Demonstrates the requirement to explicitly cast a string value exceeding 8000 bytes as Varchar Max to prevent truncation. Thanks for the education.
October 8, 2009 at 2:50 am
Cut and paste the code in QA and you get:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'max'.
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near 'max'.
Server: Msg 137, Level 15, State 1, Line 3
Must declare the variable '@str'.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
October 8, 2009 at 3:00 am
It's a SQL Server 2005 or above question pal. It won't work on 2000. That wasn't very clear in the question though.
October 8, 2009 at 3:06 am
If you'd given me the 18003 option, I'd have taken it
October 8, 2009 at 4:45 am
Bob Bobbity (10/8/2009)
If you'd given me the 18003 option, I'd have taken it
I must admit that would have been my favourite wrong answer if I was setting the question!
Wasn't sure why 16005 was there
Kev
October 8, 2009 at 5:41 am
Bob Bobbity (10/8/2009)
If you'd given me the 18003 option, I'd have taken it
Me too, I wouldn't have given it a second's thought. Good question!
Ron Moses
-----
a haiku...
NULL is not zero
NULL is not an empty string
NULL is the unknown
October 8, 2009 at 9:07 am
Chris Houghton (10/8/2009)
It's a SQL Server 2005 or above question pal. It won't work on 2000. That wasn't very clear in the question though.
It was a good Q for me precisely because, until very recently, I was only working w/ SQL Server releases prior to v9 (SQL2005) and still had in my head that the max. size for a VARCHAR was 8000 bytes even as I had heard about VARCHAR(MAX), which I see (e.g. see http://teratrax.com/articles/varchar_max.html) is only available as of SQL2005.
October 8, 2009 at 9:28 am
Good question. But the real googly would have been if 18003 was included in options in place of 8000 or 16005.
Then you would have seen more wrong answers than correct. 10000 + 8000 + 1 + 1 + 1 = 18003.
SQL DBA.
October 8, 2009 at 9:57 am
If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.
I guess the documentation is wrong...it says truncation occurs only if you're NOT using varchar(max) or nvarchar(max) - they need to remove the NOT.
Joshua Perry
http://www.greenarrow.net
October 8, 2009 at 10:20 am
The string being replicated must be a varchar(max) not the result string.
Select Len(Replicate( Cast('#' as varchar(max)), 10000)) -- 10,000
Select Len(Cast( Replicate( '#', 10000) as varchar(max) )) -- 8,000
Seems like an odd choice of convention unless it is for some backward compatability. Perhaps some people were relying on the truncation behavior.
October 8, 2009 at 11:29 am
I've altered the question to say SQL 2005. However varchar(max) wasn't available before that version. We typically don't make up functions in the questions, only the answers 😉
October 8, 2009 at 11:34 am
Nice question. Hopefully I'll trip over this one enough times to prevent me from making that mistake when it counts.
Thanks,
October 8, 2009 at 5:47 pm
October 13, 2009 at 4:40 pm
Joshua Perry-333829 (10/8/2009)
If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.
I guess the documentation is wrong...it says truncation occurs only if you're NOT using varchar(max) or nvarchar(max) - they need to remove the NOT.
Nope. The doc is correct. When you code REPLICATE('#',10000)
then the string expression '#' is seen as a single character, not a string of type varchar(max). The tricky part of the QOD was that even though the output of the function was being concatenated into a varchar(max), the string being repeated in that first REPLICATE was not a varchar(max), so its output is truncated to 8000. The second REPLICATE casts the '#' as varchar(max) and so the function output is not truncated.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply