• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

converting varchar() to Text

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.
2 Solutions
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?
gotaquestionAuthor Commented:
I did that and it truncated the info. so now what?
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.  
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

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 . . .

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.
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 
Post your closing recommendations!  No comment means you don't care.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now