﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Discuss content posted by Peter Petrov / Article Discussions by Author  / Data Length / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 16:10:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>Excellent discussion guys, really like the way it evolved.  I got it wrong, because I didn't read the question properly - had same problem at school ;) - and put 4000 doh!Good work, thanks, covered a lot more than just the answer :)</description><pubDate>Tue, 10 Feb 2009 07:11:01 GMT</pubDate><dc:creator>Chris-843659</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>[quote][b]Shaun McGuile (2/2/2009)[/b][hr]Hugo: so what you are saying is that REPLICATE uses varchar sizing i.e. 8000 and not the destination size unless you explicitly instruction the function to use something else as shown in your code. ;)Dumb function ! :P[/quote]Hi Shaun,I'd use different words, but basically you're right.And by the way, this is not limited to REPLICATE but applies to all expressions in SQL Server. The division below discards the fraction because the dividend of two integers is defined to be an integer, regardless of whether the result will later be assigned to a float variable.[code]DECLARE @i1 int, @i2 int, @f1 float;SET @i1 = 10;SET @i2 = 3;SET @f1 = @i1 / @i2;SELECT @f1;[/code]And as far as I can remember, all other programming languages I have been involved with behave similarly.</description><pubDate>Mon, 02 Feb 2009 11:25:56 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>Thanks Robbert and Hugo for helping me out.</description><pubDate>Mon, 02 Feb 2009 10:58:40 GMT</pubDate><dc:creator>karthik.nallajalla</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>Hugo: so what you are saying is that REPLICATE uses varchar sizing i.e. 8000 and not the destination size unless you explicitly instruction the function to use something else as shown in your code. ;)Dumb function ! :P--Shaun</description><pubDate>Mon, 02 Feb 2009 09:29:02 GMT</pubDate><dc:creator>Shaun McGuile</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>[quote][b]Shaun McGuile (2/2/2009)[/b][hr]REPLICATE function is obviously broken then.[/quote]Hi Shaun,No, it's not. It just adheres to the standard operating procedure of datatype in = datatype out.[code]CREATE TABLE #t         (ID int,          LongName nvarchar(MAX));INSERT #tVALUES (1, REPLICATE(CAST(N'A' AS nvarchar(MAX)), 4000000));SELECT DATALENGTH(LongName) FROM #t WHERE ID = 1;[/code]Returns 8000000.</description><pubDate>Mon, 02 Feb 2009 08:50:08 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>Don't forget that the DATALENGTH function is different from the LEN function.DATALENGTH returns the number of [i]bytes[/i] versus LEN which returns the number of [i]characters[/i]. That is one of the tricky parts of this particular question.;)</description><pubDate>Mon, 02 Feb 2009 08:16:45 GMT</pubDate><dc:creator>Jeff Kunkel-812485</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>REPLICATE function is obviously broken then.it only gives 8,000 repititions and thus must be geared to old SQL 2000 varchar max size of 8000.Naughty MS have not updated the function to deal with the new data types.--Shaun</description><pubDate>Mon, 02 Feb 2009 07:41:09 GMT</pubDate><dc:creator>Shaun McGuile</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>[quote][b]karthik.nallajalla (2/1/2009)[/b][hr][b]REPLICATE( N'A', 4000000));[/b] what is the meaning of N in N'A'? is it the maximum length. I executed the below statements,INSERT #t VALUES (1, REPLICATE( N'A', 4000000));INSERT #t VALUES (2, REPLICATE( 'A', 4000000));SELECT DATALENGTH(LongName) FROM #tThe result of the SELECT is  800016000I don't know why i got 16000. Can anyone explain me?[/quote]Hi Karthik,Yes, I can.The result of REPLICATE depends on the first argument. If it's varchar(MAX) or nvarchar(MAX), the result will also be [n]varchar(MAX) and the string won't be truncated. However, a string constant will never be considered ..(MAX) - well, maybe if the string constant is more than 4000 (nvarchar) or 8000 (varchar) characters long, I never tried that. :)For REPLICATE(N'A', 4000000), the input string is nvarchar(something other than max), so the result can not be longer than the longest nvarchar, which is nvarchar(4000). That's 8000 bytes, since two bytes are used for each Unicode character. The implicit cast to nvarchar(MAX) when storing the result in the table does not affect the data length.For REPLICATE('A', 4000000), the input string is varchar(something other than max) -- note no N in front of varchar!!--. The result will be the longest varchar, which is varchar(8000). That's 8000 bytes as well, of course. But the implicit cast to nvarchar(MAX) will translate each of the 1-byte non-unicode characters to its two-byte unicode equivalent, bumping the data length to 16,000 bytes. And since nvarchar(MAX) has no problem storing an 8,000 byte unicode string, it will.</description><pubDate>Mon, 02 Feb 2009 01:33:56 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>The N'A' means that 'A' is an Unicode string constant.Unicode is using 2 bytes to encode each character.The DATALENGTH function returns the number of bytes used to represent the expression.So in this case, you have to multiply the number in the REPLICATE function by 2.HTH,Robbert</description><pubDate>Mon, 02 Feb 2009 00:31:11 GMT</pubDate><dc:creator>Robbert Hof</dc:creator></item><item><title>RE: Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>[b]REPLICATE( N'A', 4000000));[/b] what is the meaning of N in N'A'? is it the maximum length. I executed the below statements,INSERT #t VALUES (1, REPLICATE( N'A', 4000000));INSERT #t VALUES (2, REPLICATE( 'A', 4000000));SELECT DATALENGTH(LongName) FROM #tThe result of the SELECT is  800016000I don't know why i got 16000. Can anyone explain me?</description><pubDate>Sun, 01 Feb 2009 16:27:47 GMT</pubDate><dc:creator>karthik.nallajalla</dc:creator></item><item><title>Data Length</title><link>http://www.sqlservercentral.com/Forums/Topic647627-623-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/65559/"&gt;Data Length&lt;/A&gt;[/B]</description><pubDate>Sat, 31 Jan 2009 20:37:36 GMT</pubDate><dc:creator>Peter Petrov</dc:creator></item></channel></rss>