August 24, 2005 at 6:38 pm
Hi Friends
I want to change datatype of a column from text to varchar.I know i can do one at a time.but there r too many to do.
is there any quicker way changing datatypes in a specified table.
Thanks
August 24, 2005 at 9:55 pm
Can you show us exactly what you are trying to do?
August 24, 2005 at 10:01 pm
probably needs to change datatype of a bunch of columns in a table in one step.
August 24, 2005 at 10:27 pm
August 25, 2005 at 8:49 am
Check BOL under modfying Default definitions. The short answer is that you can not change a column that has a data type of text.
HTH
Mike
August 25, 2005 at 3:25 pm
August 25, 2005 at 5:21 pm
OOPS , sorry. This may help.
Mike
From BOL
ALTER COLUMN
Specifies that the given column is to be changed or altered. ALTER COLUMN is not allowed if the compatibility level is 65 or earlier. For more information, see sp_dbcmptlevel.
The altered column cannot be:
Some data type changes may result in a change in the data. For example, changing an nchar or nvarchar column to char or varchar can result in the conversion of extended characters. For more information, see CAST and CONVERT. Reducing the precision and scale of a column may result in data truncation.
August 25, 2005 at 5:39 pm
August 25, 2005 at 5:57 pm
Cannot alter column 'creator' because it is 'text'.
August 25, 2005 at 6:03 pm
but i can change in EM though
August 26, 2005 at 4:19 am
You can also change it via script, but you need a workaround for it
IF OBJECT_ID('showme') > 0
DROP TABLE showme
CREATE TABLE showme (c1 TEXT)
GO
INSERT INTO showme SELECT 'Hello World'
ALTER TABLE showme ADD c2 VARCHAR(500)
GO
UPDATE showme SET c2 = SUBSTRING(c1,1,500)
GO
ALTER TABLE showme DROP COLUMN c1
EXEC sp_rename 'showme.c2', 'c1', 'COLUMN'
SELECT * FROM showme
EXEC sp_columns 'showme'
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
August 26, 2005 at 4:50 am
Yes it can be change via EM
Yes it gives the following error
"Data may be lost converting column 'usernsme' from 'varchar(50)' " to text
But no data is lost if there are atleast 5000 records
For more records i h.v not tried
So don't worry
it can be possible
Smruti
August 26, 2005 at 6:43 am
Nice to see you back from whatever you were doing .
August 26, 2005 at 6:43 am
If you drop from text to varchar 500, any record that contains 501+ characters will be truncated to 500. That's where you would have a loss of data.
August 26, 2005 at 8:04 am
The same fact still applies, wheter it be a script or using EM..
You cannot change (ie alter) a column that is type text to varchar. Frank's script does not change the column, nor does EM.
The only thing you can do (and that is what Frank's script and EM does) is to create a new column as varchar, copy data from the textcolumn over to the varchar column (which will fit as many chars as the varchar is long) and then delete the text column...
The difference may be subtile, but there's quite a big leap from 'change/alter' to 'createNew-copy-deleteOld', at least in the context of SQL Server.
/Kenneth
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply