Solved

converting varchar() to Text

Posted on 2002-07-16
7
637 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
ID: 7157850
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
ID: 7157906
I did that and it truncated the info. so now what?
0
 
LVL 5

Expert Comment

by:CFXPERT
ID: 7157915
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 5

Assisted Solution

by:spcmnspff
spcmnspff earned 50 total points
ID: 7157931
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
ID: 8660695
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
ID: 9280120
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

830 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