Solved

How concatenate fields in SQL without losing data?

Posted on 2002-04-16
2
934 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:pacumming
2 Comments
 
LVL 1

Accepted Solution

by:
kfehriba earned 125 total points
ID: 6945899
The problem as you have stated is the character limitation with the varchar (8000) or even worse with nvarchar(4000).

What I would do is create a separate table with the searchable text field and a key, and keep that key on this record without the searchable text being stored on this record. Then you could join based on those fields and be able to do your search. Of course even then your searchable text field would be 8000 minus the 8 bytes for the key field.

Kent
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 6945957
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).
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

778 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