Solved

Rtrim function HElppppppppppppp

Posted on 2003-12-11
7
678 Views
Last Modified: 2012-06-21
I have about 36 fields in a table. All 36 fields need to be trimed. (more specific Rtrim). Is there any way I can Rtrim all 36 fields in one shot instead of typing rtrim(fieldname) for all 36 fields.

Thanks
0
Comment
Question by:proudpaki9
[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
7 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9922188
nope
sorry
UPDTE BLah set = Rtrim(Blah) sorry
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9922193
You couldve done it by the time it took to post this question and get the answer. How about you type the fields here and il write the query for you
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 9922195
declare @sqlstr varchar(8000)

select @sqlstr=@sqlstr + 'update mytable as X set [' + column_name + '] = rtrim([' + column_name + ']),'
  from information_schema.columns
 where table_schema='dbo'
 and table_nam='mytable'
 and data_type like '%varchar%'

then you can execute it,.,

you could also build the where clause to see if the row actually needs to be updated..
but with 36 columns its probably best to just update them all...

 

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 4

Expert Comment

by:astankovic
ID: 9922198
I don't think you can.
What you can do is generate a script that will generate the sql statement with necesary fields having rtrim() around them.
If that is in a stored procedure, i guess you could then execute that sql dynamically...
0
 
LVL 6

Expert Comment

by:acampoma
ID: 9922227
Here you go
drop table #sp_Columns
go
Drop Table #Columns
go
Create table #Columns (ColumnId Int Identity(1,1),ColumnNameTxt Varchar(50))
go
Declare @MaxCol int
DEclare @CurrCol Int
DEclare @TableName Varchar(50)
Declare @ColumnName Varchar(50)
declare @SQLText varchar(2000)
set @TableName='T_RISK'



Create Table #sp_Columns (TABLE_QUALIFIER varchar(100),
      TABLE_OWNER varchar(100),
      TABLE_NAME varchar(100),
      COLUMN_NAME varchar(100),
      DATA_TYPE varchar(100),
      [TYPE_NAME] varchar(100),
      [PRECISION] varchar(100),
      LENGTH Int,
      SCALE varchar(100),
      RADIX varchar(100),
      NULLABLE varchar(100),
      REMARKS varchar(3500),
      COLUMN_DEF varchar(100),
      SQL_DATA_TYPE varchar(100),
      SQL_DATETIME_SUB varchar(100),
      CHAR_OCTET_LENGTH varchar(100),
      ORDINAL_POSITION varchar(10),
      IS_NULLABLE varchar(10),
      SS_DATA_TYPE  varchar(100))

Insert Into #sp_Columns exec  sp_Columns @TableName
Insert Into #Columns
select COLUMN_NAME from #sp_Columns


Select @MaxCol=Max(ColumnId),@CurrCol=Min(ColumnId)from #Columns
Set @SQLText='Select '
While @CurrCol<=@MaxCol Begin
select @ColumnName=ColumnNameTxt from #Columns where ColumnId=@CurrCol
set @SQLText=@SQLText+ 'rtrim('+@ColumnName+') as ' +@ColumnName
if @CurrCol<>@MaxCol Begin
      set @SQLText=@SQLText+','
      End      
set @CurrCol=@CurrCol+1
end
Set @SQLText=@SQLText+' from '+@TableName
exec ( @SQLText)
0
 
LVL 6

Expert Comment

by:acampoma
ID: 9922240
There is a line set @TableName='T_RISK'
set the tablename to whatever you want

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 9922402
Yep, best way is to generate the statement, then copy/paste to run it:


DECLARE @sql VARCHAR(8000)

SET @sql = 'UPDATE yourTableName ' + CHAR(13) +
    'SET '

SELECT @sql = @sql + '[' + column_name + '] = RTRIM([' + column_name + ']), ' + CHAR(13) + SPACE(4)
FROM INFORMATION_SCHEMA.columns
WHERE table_name = 'yourTableName'
AND data_type LIKE 'varchar%'

SET @sql = LEFT(RTRIM(@sql), LEN(@sql) - 3)

PRINT @sql
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

623 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