Solved

RTRIM LTRIM for TEXT datatype

Posted on 2008-10-03
17
2,580 Views
Last Modified: 2012-08-14
Hi Experts,

I am using a VARCHAR(MAX) parameter in one of the stored procedures. Recently i discovered it might get bigger than 8000 characters. So i changed that parameter to TEXT. But problem is i cannot use RTRIM, LTRIM functions on TEXT datatype. Is there any work around for this problem?

Thanks
Raju
0
Comment
Question by:srivatsavaye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22635435
varchar(max) supports text up to 2GB.  No need to use text.
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22635548
Yep!! VARCHAR supports text up to 2GB but my character string length is more than 8000. VARCHAR truncates anything more than 8000 characters
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22635816
not varchar(max).
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22635851
varchar(max) should be used everywhere that text used to be used.  See the excerpt below from BOL.


ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. For more information, see Using Large-Value Data Types.
http://msdn.microsoft.com/en-us/library/ms187993(SQL.90).aspx
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22635910
But my parameters length is more than 8000. how would i use varchar?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22635962
declare your parameter as data type varchar(max)

ex.

Create procedure up_Proc1
  @Param varchar(max)
as
select @Param
go


@Param will accept up to 2GB of data, not 8000 characters.
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22636056
That is exactly the way i declared my parameter.

you are saying that even if the string length is more than 8000, it will show everything if the size is below 2GB? It is not happening.

Here is little bit more detail of my problem:

I have this report (SSRS) which has a parameter called Resources. i pass in this parameter to a stored procedure. Parameter is just bunch of comma separated Id's. When i print this parameter using TEXT(storage space is 2GB for this too) datatype, length of the parameter is 8792 and if i print it using VARCHAR(MAX), length is 8000. So it is clearly truncating the parameter to 8000 characters unless i am missing something.

Thanks
Raju
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22636157
So when you execute in grid view or text view?

Tools > Options
Query Results > SQL Server > Results to #### (text/grid)

Text will only display the first 8192 characters to screen.  that is a limitation of the query tool.  But the procedure will output all of the data when running.
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22636329
I am not displaying it in SSMS. i am doing it on my report
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22636354
What data type is your field in your table that you are returning?
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22636379
It is not a datatype or anything. i just tried this for testing purposes

create proc sp_proc
@resources VARCHAR(MAX) (TEXT)

AS

select @Resources as Res

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22636413
Not sure what you're trying to do there:


create proc sp_proc
@resources VARCHAR(MAX)

AS

select @Resources as Res
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22636428
i was just trying to prove that VARCHAR truncates the string after 8000 characters. try it for your self if you want.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22636487
No... what are you doing trying to put (text) after varchar(max)?


varchar(MAX) does not truncate.  If you declare a parameter as varchar(8000)


Try this:



declare @a varchar(max)
set @a=replicatE('a',8000)
set @a=@a+@a
select len(@a)


replicate won't product a string longer than 8000, but that's not a limitation of the data type, it's a limitation of the replicate function.
0
 
LVL 9

Author Comment

by:srivatsavaye
ID: 22636686
The example you gave doesn't prove that VARCHAR(MAX) doesn't truncate after 8000 characters. can you give me an example where varchar(max) accommodates more than 8000 characters?

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22636766
Are you serious?  @a has a length of 16000 if you run that.
0
 
LVL 9

Author Closing Comment

by:srivatsavaye
ID: 31502821
Sorry abut that!!! it is 16000. thanks for your help
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Parse this column 6 35
SQL Query Syntax error after > 11 41
Applying Roles in Common Scenarios 3 19
T-SQL: How to extract records into a new table 7 21
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question