Solved

How concatenate fields in SQL without losing data?

Posted on 2002-04-16
2
905 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:ScottPletcher
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now