Link to home
Start Free TrialLog in
Avatar of pacumming
pacummingFlag for United States of America

asked on

How concatenate fields in SQL without losing data?

SQL Server DataTypes and Conversion and Concatenation
SQL Server 7.0 with SP3 in Windows 2000
 
I am working with 12 Fields.  I want to be able to concatenate the first 11 into the 12th Field.
 
Here are the field names and datatypes
1.)  [ACCESSION_NUMBER] [varchar] (12)
2.)  [TITLE] [varchar] (1000)
3.)  [AUTHOR] [varchar] (255)
4.)  [DOCUMENT_NUMBER] [varchar] (50)
5.)  [DOCUMENT_DATE] [varchar] (255)
6.)  [DOCUMENT_YEAR] [int]
7.)  [COMPANY_SOURCE] [varchar] (255)
8.)  [LOCATION] [varchar] (255)
9.)  [DOCUMENT_TYPE] [varchar] (255)
10.) [ABSTRACT] [text]
11.) [DESCRIPTORS] [text]
 
and the 12th field
12.) [SEARCHTEXT] [varchar] (8000)
 
The problem is I am converting the 10th and 11th fields into varchar(1500) fields.  This causing a loss of data. I can not increase the number since the character limitation on varchar is 8000.
 
I haven't been able to convert all the fields to the [text] datatype during the concatenation. I'm not trying to use a computational field, I'm running a stored procedure that would do the concatenation.
 
The stored procedure is:
CREATE PROCEDURE dbo.spSearchText
AS
UPDATE dbo.tblDocuments
SET SEARCHTEXT = ACCESSION_NUMBER + ' ' + TITLE + ' ' + AUTHOR + ' ' + DOCUMENT_NUMBER + ' ' + ' ' + DOCUMENT_DATE + ' ' + COMPANY_SOURCE
 + ' ' + LOCATION + ' ' + DOCUMENT_TYPE + ' ' + CONVERT(varchar(3000), ABSTRACT) + ' ' + CONVERT(varchar(3000), DESCRIPTORS)
GO
 
How can concatenate without lossing data? What Data Type should I convert all the fields too, before the concatenation?
 
Any help would be appreciated.
Thanks in advance,
Peter
ASKER CERTIFIED SOLUTION
Avatar of kfehriba
kfehriba

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Pletcher
Kfehriba is correct, I think, you need to create a separate table for this columns.  Especially because there is a ROW length limitation as well, of about 8060 bytes.

Since the first 11 columns take up about 5,360 bytes (12 + 1000 + 255 + 50 + 255 + 4 + 255 + 255 + 255 + 1500 +
1500 = 5341, plus there are at least 10 bytes of overhead per row), that leaves you less than 3000 bytes in that row.  If you were to place the concatenated result in another table, you should even be able to increase the length pulled from the text columns to about 2800 bytes each (12 + 1000 + 255 + 50 + 255 + 4 + 255 + 255 + 255 + 2800 + 2800 = 7941).