September 13, 2007 at 8:05 pm
I am trying to get a simple RTRIM function working. I am running the following query and outputting the results to text (I need the results in text outputted to QA)
select
RTRIM(filename), [name] from sysfiles
The filename column is an nvarchar(520) in SQL 2005. In the results set the column is returned with trailing blanks as if the RTRIM function is ignored.
I tried the following and this also does not work:
select
SUBSTRING(filename, 1, LEN(filename)), name from sysfiles
however if I use the LEN function to calculate the length, e.g. 40 characters and then run
select SUBSTRING(filename, 1, 40), name from sysfiles
I then get the desired result. But I need this to be dynamic. I won't always know the length.
How can I trim the trailing blanks on the filename column? Why doesn't RTRIM work?
September 13, 2007 at 10:21 pm
Oh... it works... it's just the text mode display has a mind of it's own when it comes to calculated column widths... so you need to do something like this...
DECLARE @MaxWidth INT SELECT @MaxWidth = MAX(LEN(FileName)) FROM Master.dbo.SysFiles DECLARE @SQL VARCHAR(8000) SET @SQL = 'SELECT LEFT(FileName,'+STR(@MaxWidth)+') FROM Master.dbo.SysFiles' EXEC (@SQL)
--Jeff Moden
Change is inevitable... Change for the better is not.
September 14, 2007 at 8:17 am
Peter... try SUBSTRING with a variable for length or RTRIM in the text mode on a wide column to see what the OP means... displayed column comes out too wide...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 14, 2007 at 4:04 pm
You can use simple left function:
select name=left(name, 30)
from sysfiles
Just make sure that the longest file name is smaller than this number.
It is sometimes inconvenient when SSMS allocates 255 character places for values which are much smaller than that. Alternatively you can use grid mode by pressing Ctrl D.
September 14, 2007 at 5:09 pm
Yes... that will work but it's not "dynamic" like the Op asked... if the length is a variable, it will not display as expected in the Text Pane.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 16, 2007 at 3:55 am
How about 2 * LEN() because it NCHAR?
N 56°04'39.16"
E 12°55'05.25"
September 16, 2007 at 7:54 am
Heh... did you try it?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 16, 2007 at 8:46 pm
Jeff - thank you for your help, your solution works a treat and is dynamic as required. It is slightly annoying that these extra lines of code and dynamic SQL is required. Any ideas why RTRIM appears not to work when outputting to text?
September 17, 2007 at 4:26 am
RTRIM does work. You are just mistaken in assuming that SQL Server will adjust the column width to match the maximum size of the trimmed column. SQL Server just makes the output column the size of the source column.
September 17, 2007 at 10:18 am
Keep it simple:
declare @Filename nvarchar( 260 ), @Name nvarchar( 128 ); select @Filename = rtrim( filename ), @Name = rtrim( name ) from sysfiles; select @Filename as Filename, @Name as Name;
Simple, dynamic, does everything you want.
Tomm Carr
--
Version Normal Form -- http://groups.google.com/group/vrdbms
September 17, 2007 at 1:31 pm
The script on the link below may be useful for what you are trying to do.
Get Server Database File Information
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=89058
September 17, 2007 at 4:04 pm
It just occurred to me that you may want more than one row. In that case, select into a temp table or table variable.
declare @Sysfiles table ( [filename] nvarchar( 260 ), [name] nvarchar( 128 ) ) insert into @Sysfiles select rtrim( filename ), rtrim( name ) from sysfiles; select * from @Sysfiles;
Tomm Carr
--
Version Normal Form -- http://groups.google.com/group/vrdbms
September 17, 2007 at 5:43 pm
Michaeal said it right...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 17, 2007 at 5:43 pm
How does that produce the dynamic sizing the OP asked for? That code works great on the input but does nothing for formatting the output in the text window.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply