473,804 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compile/combine the contents of several records.

I have the following table;

CREATE TABLE [x_Note] (
[x_NoteId] [int] IDENTITY (1, 1) NOT NULL ,
[Note] [varchar] (7200) COLLATE SQL_Latin1_Gene ral_Pref_CP1_CI _AS NOT
NULL ,

CONSTRAINT [PK_x_NoteId] PRIMARY KEY CLUSTERED
(
[x_NoteId],
) WITH FILLFACTOR = 90 ON [USERDATA] ,

) ON [USERDATA]
GO

My clients want me to take the contents of the Note column for each row
and combine them. In other words, they basically want:

Note = Note [accumulated from previous rows] + Char(13) [because they
want a carriage return] + Note [from current record].

What is the most efficient and relatively painless way to do this? I
think it might require a cursor, but I'm not sure if there is a more
elegant set-based method to make this happen.

Dec 5 '05
14 2415
One more problem: there could be hundreds of categories, and we dont
know in advance how many categories there will be.

Dec 7 '05 #11
im************* ******@yahoo.co m wrote:
One more problem: there could be hundreds of categories, and we dont
know in advance how many categories there will be.


What's the problem? With the script I posted you don't need to know the
number of categories. You do need to set some upper limit on the number
of notes for each category but given that the maximum size of a VARCHAR
is 8000 and that your note column is 7K characters that may not be a
problem either - the number of notes you can support with this or any
other SQL query is possibly quite small anyway.

If you want to concatente the notes to a TEXT / NTEXT column then I
think you really will have to use a cursor.

--
David Portas
SQL Server MVP
--

Dec 7 '05 #12
im************* ******@yahoo.co m (im************ *******@yahoo.c om) writes:
Now, I have NO idea how to pull this off. I was considering nested
cursors, but I'm not sure if that is the best way, or even a practical
way. Help!!!


Before just posting "I can't do this, I can't do that", how about doing
some research. How long will these combined texts be? Since the notes
are declared as varchar(7200), one suspects that the total length may
exceed 8000 chars. But is it really so? What does:

SELECT NoteCategory, SUM(len(Note) + 1)
FROM x_Note
ORDER BY 2 DESC

return?

If there commonly are categories where the total length is > 8000, you
will have to run a cursor, and accumlate data into a new table, where
the concatenated note is written into a text column using the WRITETEXT
command. Quite painful. You don't need nested cursors though, your loop
only needs some logic to recognize that you have entered a new NoteCategory.

By the way, within each category, in which order are you supposed to
concatenate them? By x_NoteId? Are you sure that gives a correct result?

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 7 '05 #13
That code was amazing! We are running tests now, but it looks like it
works very well with just minor adjustments. Thanks very much!!!

By the way, would books would you recommend for enhancing one's
knowledged of SQL programming in general?

Dec 8 '05 #14
im************* ******@yahoo.co m (im************ *******@yahoo.c om) writes:
By the way, would books would you recommend for enhancing one's
knowledged of SQL programming in general?


There is a book by SQL Server MVPs Tom Moreau and Itzik Ben-Gan of
which the exact title slips me now, but I think it's something like
"Advanced SQL Server Programming". But just search on the author's
names on Amazon or similar and you should find it.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 8 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3167
by: Ken Fine | last post by:
I want to take the contents of many fields of various datatypes (varchars and text) and combine them into a single "junk" field that I will perform SQL Server free text searching upon, e.g.: tblArticles Title Subtitle ArticleText CombinedField
9
3735
by: Steve Jorgensen | last post by:
Hi all, I'm working on the schema for a database that must represent data about stock & bond funds over time. My connundrum is that, for any of several dimension fields, including the fund name itself, the dimension may be represented in different ways over time, and may split or combine from one period to the next. When querying from the database for an arbitrary time period, I need the data to be rolled up to the smallest extent...
8
7106
by: mark | last post by:
Access2000 How do I write a query that combines the CTC field from each record below into one record? I need to concatenate the CTC field with a separator, like below: BattID VehicleID STDATE STTIME CTC LKO500HF 00000000 10/27/2003 4:13:51 AM 4 LKO500HF 00000000 10/27/2003 5:13:51 AM 5 LKO500HF 00000000 10/27/2003 10:13:51 AM 6 LKO500HF 00000000 10/27/2003 11:13:51 AM 4
1
2996
by: snOOp | last post by:
I am trying to combine the data from two similar tables into one query, but I need for all of the records from both tables to show up and I want the ones that have matching 'emplid' to be combined into one record showing both the 'empstatus' and 'strole' fields. The following query works, but does not combine the matching records: SELECT givenname, sn, empstatus, emplid, ssn FROM dbo_EmpData WHERE empstatus='A' UNION ALL SELECT...
3
1499
by: steve_h | last post by:
I think the subject says it all, but just in case: I know that I can call my own methods during an XSL transformation using <xsl:value-of select="myObject.someMethod(arg1)" /> having done something like: dim xslTrans as System.Xml.Xsl.Xsltransform
1
2198
by: Beowulf | last post by:
I have a report laid out in Design View as shown at the end of this message. I have code that performs the following steps: 1. In main report's Report_Open(), DELETE any old rows in tblTOC for this username. 2. In main report's CategoryHeader_Format(), add a row to tblTOC with the current category name and the current page number. 3. In the table of contents subreport, Cancel if NoData event fires.
4
3684
by: musicloverlch | last post by:
I have a table with multiple records in it. I am being forced to combine multiple records into one record in order to be uploaded to a website, and I only get one record per client. How can I systematically combine multiple records into 1? The table looks like this Client ID Service Status 1 Doc Pending 1 Test Pending
1
2238
by: rdraider | last post by:
Hi all, We have an app that uses SQL 2000. I am trying to track when a code field (selcode) is changed on an order which then causes a status field (status) to change. I tried a trigger but the app may use 2 different update statements to change these fields depending on what the user does. When the trigger fires (on update to selcode), the status field has already been changed. So my trigger to record the changes from inserted and...
1
3419
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script below. My problem is if I don’t use the WHERE clause in my script below, the script can query up to 3 records with the same “LoanNo” due to the fact that there can be up to 3 “TypeCodes” in each Table (Borrower, Co-Borrower 1 and Co-Borrower 2...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.