Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I have a Table named as T1 which has 50 columns.

I need to write a query to change the datas in the 50 columns to uppercase by using a single query.

with expectation,
Viswanathan.M
Posted
Updated 9-Apr-11 2:43am
v2
Comments
Dalek Dave 9-Apr-11 8:43am    
Edited for Readability.

Something like:
SQL
UPDATE MyTable
   SET Col1 = UPPER(Col1),
       Col2 = UPPER(Col2), ....
 
Share this answer
 
Comments
Viswanthan.M. 9-Apr-11 7:16am    
Thank You for your reply. but if i have 50 columns,should i write this query for 50 columns?
Is there any other query?
with expectation,
Viswanathan.M
Wendelius 9-Apr-11 7:19am    
Don't quite understand your question. You add all the columns in the SET clause. When you have done this you have a single UPDATE statement which will update all the columns to uppercase in a single run.
SQL
SELECT 'UPDATE YOURTABLENAME SET ' + name + '=' + 'UPPER(' + name + ')'
FROM syscolumns
WHERE id = object_id('YOURTABLENAME')


This will create a query just copy-paste it and run it.

NOTE : Remove identity column if any. Otherwise it will raise an error.

Hope it helps.
 
Share this answer
 
Comments
Dalek Dave 9-Apr-11 8:43am    
Good Call.
That's Aragon 9-Apr-11 8:51am    
Thank you DD :)
This query will do it.
declare @sql        VARCHAR(8000)
declare @tableName  SYSNAME
set     @tableName  = 'yourTableNameGoesHere'

set     @sql = 'UPDATE ' + @tableName + ' SET ' + 
        (
        select  QUOTENAME(COLUMN_NAME) + ' = UPPER(' + QUOTENAME(COLUMN_NAME) + '), '
        from    INFORMATION_SCHEMA.COLUMNS
        where   TABLE_NAME = @tableName
                AND
                DATA_TYPE IN('char','varchar','nchar','nvarchar')
        order
        by      ORDINAL_POSITION
        for xml path('')
        )
---- remove last ", "
set     @sql = LEFT( @sql, LEN(@SQL)-1)
print @sql
---- uncomment next line to execute the query
--exec (@sql)
 
Share this answer
 
update table_name set column_name=upper(column_name)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900