October 27, 2009 at 6:48 am
Trimming a Text column, even when converting it to CHAR(20000) is not possible because RTRIM supports a max of 8000 characters.
Any tips how to truncating all trailing blanks on a Text or NText column?
October 27, 2009 at 7:51 am
There is likely a more efficient way... but this may work:
SELECT REVERSE(LTRIM(REVERSE(yourcolumn)))
October 27, 2009 at 8:00 am
works fine for me when i use a convert to varchar(max):
--pad my table with too much whitespace
update MyTable set myTextColumn = isnull(convert(varchar(max),myTextColumn),'') + replicate(' ',10000)
--use rtrim to get select without the whitespace
select rtrim(convert(varchar(max),myTextColumn)) from MyTable
--update the table to get rid of whitespace
update MyTable set myTextColumn = rtrim(convert(varchar(max),myTextColumn))
Lowell
October 27, 2009 at 8:05 am
Check the Datalength of that Lowell. I think you need to cast the ' ' inside that replicate to varchar(max) or it'll cap out at 8K.
October 27, 2009 at 8:20 am
ahh your right...you can change to varhcar(max), and make things bigger than 8000, but only in 8000 chunk bites.
here's my test code;
create table MyTable (
MyTableId int identity(1,1) not null primary key,
myTextColumn text)
insert into MyTable(myTextColumn)
SELECT replicate('bananas ',10000) union all
SELECT replicate('apples ',10000) union all
SELECT replicate('oranges ',10000)
select datalength(myTextColumn) from MyTable
--ugg! Garadin is right...it truncates to about 8000 chars
update MyTable set myTextColumn = isnull(convert(varchar(max),myTextColumn),'') + replicate(' ',10000)
--this did make it 16000 chars
select datalength(myTextColumn) from MyTable
select rtrim(convert(varchar(max),myTextColumn)) from MyTable
update MyTable set myTextColumn = rtrim(convert(varchar(max),myTextColumn))
Lowell
October 27, 2009 at 8:20 am
Sorry, the table is a "not owned by myself".
So I can't convert the NText column to a NVarchar(max).
And the 'large value types out of row' is set to off. So the sp can't convert to varchar(max).
I think the double revers gives a handle, but the ltrim will probably stop at 8000 chars.
If I could find a way, probably with patindex, to remove the left spaces, this would be a solution. 😎
October 27, 2009 at 8:36 am
Henk Schreij (10/27/2009)
Sorry, the table is a "not owned by myself".So I can't convert the NText column to a NVarchar(max).
And the 'large value types out of row' is set to off. So the sp can't convert to varchar(max).
I think the double revers gives a handle, but the ltrim will probably stop at 8000 chars.
If I could find a way, probably with patindex, to remove the left spaces, this would be a solution. 😎
Hi Henk; you don't need to alter the tables.
I'm pretty sure if you just need the data trimmed in a stored procedure, you could use the example i posted that just SELECTS the converted value.
select rtrim(convert(nvarchar(max),mynTextColumn)) from MyTable
and i think if you want to fix the data in the table, you could use the update i posted.
update MyTable set mynTextColumn = rtrim(convert(nvarchar(max),mynTextColumn))
Lowell
October 27, 2009 at 9:02 am
Lowell (10/27/2009)
ahh your right...you can change to varhcar(max), and make things bigger than 8000, but only in 8000 chunk bites.
You can go over 8K if you cast the ' ' inside the replicate:
DECLARE @a varchar(MAX)
SET @a = REPLICATE('abakelfjafa',8000)
SELECT DATALENGTH(@a)-- 7997
SET @a = REPLICATE(CAST('abakelfjafa' AS varchar(MAX)),8000)
SELECT DATALENGTH(@a) -- 88000
SELECT DATALENGTH(@a)-- 264006
SELECT DATALENGTH(REVERSE(LTRIM(REVERSE(@a))))-- 264000
October 28, 2009 at 9:03 pm
Henk Schreij (10/28/2009)
@lowell & garadinThe combination of your tips did the trick. 🙂
Problem solved.
Thank you. 😎
Cool... can you post the code that you ended up with? Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 29, 2009 at 2:20 am
Jeff Moden (10/28/2009)
Cool... can you post the code that you ended up with?
Sorry Jeff, it's not possible. 🙁
The problem comes from a complex SP a college was developing for r-trimming all the NText columns in all tables.
He got stuck in part where a RTRIM(CHAR(200000)) was truncated at 8000.
And the test code had an omission where the REPLICATE also was truncated at 8000.
In stead of CHAR(20000) he now uses VARCHAR(MAX) (Solution @lowell)
And the REPLICATE problem was solved by the CAST('...' AS VARCHAR(MAX)),8000) (Solution @Garadin)
But here is the test code:
--drop table MyTable
CREATE TABLE MyTable (
MyTableId int identity(1,1) NOT NULL PRIMARY KEY,
MyTextColumn NTEXT)
INSERT INTO MyTable(MyTextColumn)
SELECT REPLICATE(CAST(N'bananas ' AS VARCHAR(MAX)),10000)+ N'test1'+N' ' union ALL
SELECT REPLICATE(CAST(N'apples 'AS VARCHAR(MAX)), 10000) + N'test2'+N' ' union ALL
SELECT REPLICATE(CAST(N'oranges 'AS VARCHAR(MAX)), 10000)+ N'test3 '
SELECT MyTableId, datalength(MyTextColumn) AS Lng, MyTextColumn FROM MyTable
--1 160018 'bananas .. bananas test1 '
--2 140018 'apples .. apples test2 '
--3 160018 'oranges .. oranges test3 '
UPDATE MyTable SET myTextColumn = RTRIM(CONVERT(varchar(max),MyTextColumn))
SELECT MyTableId, datalength(MyTextColumn) AS Lng, MyTextColumn FROM MyTable
--1 160010 'bananas .. bananas test1'
--2 140010 'apples .. apples test2'
--3 160010 'oranges .. oranges test3'
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply