Solved

converting varchar() to Text

Posted on 2002-07-16
7
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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…

717 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