Solved

converting varchar() to Text

Posted on 2002-07-16
7
613 Views
Last Modified: 2008-02-01
I've got lots of varchar(500) in my table and the users want to store more information than 500 bytes.  I basically need a text datatype.  But when I try to convert the varchar to text it says it's going to truncate the data already in there.  How do get around this? Also, what else should I look out for.  I noticed I lose the ablity to run like the len function on a text datatype field when doing a select.  Will I still be able to index and search on the field.  etc.
thanks-
0
Comment
Question by:gotaquestion
7 Comments
 
LVL 5

Expert Comment

by:CFXPERT
Comment Utility
Here's what I would do.  The results of converting from this to that can always produce unwanted results so I would make a copy of that table call it something else then go in and change those fields to text and see what the results are.  If the result is no data loss and all is well then you're halfway home.

As far as indexing a text column I don't think you can put an index on a text datatype, however you can create a full-text index on a specific entire table.  Lets say that most of a table is all text datatype you may want to allow full-text indexing on the entire table.

searching on fields of a text datatype is certainly slower than searching on a varchar with an index, your performance will go down depending on how much text you got stored.  but sometimes we just can't get around what we need to have you know?
0
 
LVL 1

Author Comment

by:gotaquestion
Comment Utility
I did that and it truncated the info. so now what?
0
 
LVL 5

Expert Comment

by:CFXPERT
Comment Utility
To get around the truncation you can try setting up a duplicate table only with datatype text then write a transfer script to dump the data from the original table that is (varchar) into the new table and see if it preserves the data without truncating it.  
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Assisted Solution

by:spcmnspff
spcmnspff earned 50 total points
Comment Utility
First, make a backup copy of the table
Then I would export the data into a a delimited text file via bcp. Just in case, pick a funky field delimiter like '|~~ or something (this will allow comma's in you text fields without messing up the import).  Then Create a second table that looks like the first ony with the varchar fields swithced to text.  The easiest way to do this is:

Select Into MyTable Select * from MyBaqckedUpTable Where 1 = 0

Alter MyTable Alter Column MyText1 Text
(You can use EM instead of the alter table statement if you prefer)

After all the text fields are converted, BCP the data back into the new table . . .


0
 
LVL 5

Accepted Solution

by:
svid earned 50 total points
Comment Utility
1. Just alter the table and add a text column

2. update the table to set text col = varchar col (This does not truncate)

3. drop the varchar column

You are done.

As for indexes, this is from Books online

"Note Columns consisting of the ntext, text, image, or bit data types or computed columns cannot be specified as columns for an index, nor can functions be used. "

However you can still use any function by saying

select len(convert(varchar(5000), text_col))

By "converting" the text column into a varchar, you can use all the functions.

As mentioned earlier, performance is hit, but, if you need a text column, this is an easy way.
0
 

Expert Comment

by:CleanupPing
Comment Utility
gotaquestion:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

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 setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

772 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

14 Experts available now in Live!

Get 1:1 Help Now