?
Solved

converting varchar() to Text

Posted on 2002-07-16
7
Medium Priority
?
679 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
6 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Assisted Solution

by:spcmnspff
spcmnspff earned 200 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

864 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