[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How concatenate fields in SQL without losing data?

Posted on 2002-04-16
2
Medium Priority
?
987 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 500 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 70

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

650 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