Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
cjp
Yak Posting Veteran
69 Posts |
Posted - 2012-05-03 : 06:26:51
|
I am using SQLS 2008, SP 3.I have been asked to evaluate a new database containing medical outcomes for hospital patients. Unlike my usual datasets (all lower case column headings), this has been provided with a mixture of lower case and upper case column headings. I have spent some frustrating time this morning trying to find internet articles on how to convert all columns in table a1234 to lower case - most contributors say it can't be done (they advise re-making the table...) but some offer code that doesn't work.The table I am evaluating has about 400 columns - it will take me a very long time to type out a query to regenerate the table with a lower case alias for the current column headings. Can anyone suggest some code to do this automatically?I should add that my work always uses a case-sensitive character collation since several of my tables use case-sensitivity to distinguish between patients and/or diagnostic lookup codes - it really does help if I can set all column headings to lower case (not least because it frees me from the anxiety of typos causing errors when parsing column names).Thanks.Chris |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-03 : 07:05:16
|
You can use sp_rename function to rename the columns. http://msdn.microsoft.com/en-us/library/ms188351.aspxIf you want to avoid typing all the script manually, you can use the following low-tech approach. Write a query to generate the query that will rename the columns, run it, copy the output of the query and run it. For example, if you had a table named ABCD and you wanted to rename all the column names to lower case letters, you would do this:Step 1: Run this queryDECLARE @TableName VARCHAR(32) = 'ABCD';SELECT 'exec sp_rename ''' + QUOTENAME( @TableName) + '.' + QUOTENAME(COLUMN_NAME) + ''', ''' + QUOTENAME(LOWER(COLUMN_NAME)) + ''', ' + '''COLUMN'';'FROM Information_Schema.[COLUMNS]WHERE Table_Name = @TableName; Step 2: Copy the output of step 1 into a SSMS query window, look at it carefully to makesure that it is doing what you want it to do, and let it run. |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-05-03 : 07:14:37
|
You don't need to use case sensitive names in order to store case sensitive data. All your character columns can be set to use a case-sensitive collation while your database collation is case-insensitive. Unfortunately it's difficult to change the database collation once it's established, but if you can start with a new database and migrate your objects and data to it, you should have no problems.Failing that, this will generate column headings in lower case:SELECT COLUMN_NAME + ' AS ' + LOWER(COLUMN_NAME) + ','FROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME='myTable' |
 |
|
cjp
Yak Posting Veteran
69 Posts |
Posted - 2012-05-03 : 07:52:11
|
Thank you for the suggestions. I have tried sunitabeck's approach and it definitely works - but I have two observations about the output it generates:1 - If your collation is case-sensitive, you will receive an error. The solution is to type INFORMATION_SCHEMA as all upper case.2 - The columns have been renamed in lower case but the form has changed subtly but crucially: the output from sp_rename gives this (for example): exec sp_rename '[b9867].[ADMIDATE]', '[admidate]', 'COLUMN'; and testing the eventual renamed table on a create table routine shows that all the columns now have square brackets as part of the actual name; eg [[admidate]]] [varchar](50) NULL,There is also the puzzle of the third closing square bracket (there for all column names). In a normal Create Table routine from an existing table, the column names are within single square brackets - so I am at a loss here. Testing by querying for admidate generates the error that there is no such field!Can I modify the sp_rename method to avoid the generation of additional brackets?Thanks.Chris |
 |
|
cjp
Yak Posting Veteran
69 Posts |
Posted - 2012-05-03 : 07:54:04
|
I will now take a look at robvolk's idea but should add that I am stuck with the case-sensitive collation owing to the need for backwards compatibility with ancient versions of our databases.Chris |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-03 : 08:03:01
|
quote: Originally posted by cjp Thank you for the suggestions. I have tried sunitabeck's approach and it definitely works - but I have two observations about the output it generates:1 - If your collation is case-sensitive, you will receive an error. The solution is to type INFORMATION_SCHEMA as all upper case.2 - The columns have been renamed in lower case but the form has changed subtly but crucially: the output from sp_rename gives this (for example): exec sp_rename '[b9867].[ADMIDATE]', '[admidate]', 'COLUMN'; and testing the eventual renamed table on a create table routine shows that all the columns now have square brackets as part of the actual name; eg [[admidate]]] [varchar](50) NULL,There is also the puzzle of the third closing square bracket (there for all column names). In a normal Create Table routine from an existing table, the column names are within single square brackets - so I am at a loss here. Testing by querying for admidate generates the error that there is no such field!Can I modify the sp_rename method to avoid the generation of additional brackets?Thanks.Chris
I should refrain from posting untested code!! When you use sp_rename, the new column name is literally, treating the square brackets as part of the column name, which I had overlooked. So, you should change the "QUOTENAME(LOWER(COLUMN_NAME))" to LOWER(COLUMN_NAME). |
 |
|
cjp
Yak Posting Veteran
69 Posts |
Posted - 2012-05-03 : 09:54:04
|
Thanks to you both - everything now works!I have tested the fix to sunitabeck's method - works perfectly and everything ran instantly on my laptop.I have also tested robvolk's method and this, too, works beautifully - but takes a little longer because it involves re-making a table. I just had to remember to remove the final comma by hand (at the end of the re-jigged column list) before dropping the output into a make table query.Thanks again - I am so grateful to this forum.Chris |
 |
|
|
|
|
|
|