Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

69 Posts

Posted - 04/27/2013 :  18:25:54  Show Profile  Reply with Quote

declare @x varchar(max)

select @ax = REPLICATE('a',2000)
select @bx = REPLICATE('b',2000)
select @cx = REPLICATE('c',2000)
select @dx = REPLICATE('d',2000)
select @ex = REPLICATE('e',2000)

select @x = @ax + @bx + @cx + @dx + @ex

why this select SUBSTRING(@x,9000,150) dont bring to me 'eeeeeee'?
where is my mistake?

Yak Posting Veteran

69 Posts

Posted - 04/27/2013 :  18:44:11  Show Profile  Reply with Quote
Hi its me again
I found this solution.

set @x = Cast(@ax as Varchar(max)) +
Cast(@bx as Varchar(max)) +
Cast(@cx as Varchar(max)) +
Cast(@dx as Varchar(max)) +
Cast(@ex as Varchar(max))
select SUBSTRING(@x,9000,150)

now works fine,
is there another solution?
tks again

Go to Top of Page

In (Som, Ni, Yak)

17689 Posts

Posted - 04/27/2013 :  22:58:43  Show Profile  Reply with Quote
is @ax to @ex also varchar(max) ?

Time is always against us

Go to Top of Page

Yak Posting Veteran

69 Posts

Posted - 04/29/2013 :  12:02:30  Show Profile  Reply with Quote
No, they are @Ax to @ex varchar(8000)
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 04/29/2013 :  12:14:00  Show Profile  Reply with Quote
Think of it as computing the right hand side (using the data types it is given) and then assigning the result to the left hand side variable. Since all the variables on the right hand side are varchar(8000), the intermediate result it calculates is also varchar(8000).

You can get around this by doing what you did, or even simply this, which forces the intermediate result to varchar(max)

SELECT @x = CAST(@ax AS VARCHAR(MAX)) + @bx + @cx + @dx + @ex
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000