Solved

Rtrim function HElppppppppppppp

Posted on 2003-12-11
7
672 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
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:ScottPletcher
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now