Solved

How concatenate fields in SQL without losing data?

Posted on 2002-04-16
2
971 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
[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
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

690 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