Unix Technical Forum

SQL Statement Help

This is a discussion on SQL Statement Help within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, Thanks for taking the time to help me out with this. I've been pulling my hair out trying ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:30 AM
cwwilly@gmail.com
 
Posts: n/a
Default SQL Statement Help

Hello,

Thanks for taking the time to help me out with this. I've been pulling
my hair out trying to figure this out.

SERVER: Microsoft SQL Server 2000

TABLE STRUCTURE:
terms_no varchar 10
note_no int 4
note_text varchar 60

SAMPLE DATA:
terms_no | note_no | note_text
2 | 1 | This is a test.
2 | 2 | This is a test second line.
3 | 1 | Another test.

THE ISSUE:
There can be multiple note_text(s) for each term_no, which can be
sorted by the note_no. I would like to take each and combine them into
one string. There can be an unlimited amount of note_no(s) and
term_no(s).

DESIRED RESULT:
If the terms_no is 2 then combine the two lines into one string like
the following:

note_text_combined
This is a test. This is a test second line.

Any ideas on how I can accomplish this through SQL I would very much
appreciate it. If I had the choice to redesign this table I would
without question, however, it can't be done.

Thanks again ! I really appreciate it !

cwwilly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:30 AM
Erland Sommarskog
 
Posts: n/a
Default Re: SQL Statement Help

(cwwilly@gmail.com) writes:
> Thanks for taking the time to help me out with this. I've been pulling
> my hair out trying to figure this out.
>
> SERVER: Microsoft SQL Server 2000
>
> TABLE STRUCTURE:
> terms_no varchar 10
> note_no int 4
> note_text varchar 60
>
> SAMPLE DATA:
> terms_no | note_no | note_text
> 2 | 1 | This is a test.
> 2 | 2 | This is a test second line.
> 3 | 1 | Another test.
>
> THE ISSUE:
> There can be multiple note_text(s) for each term_no, which can be
> sorted by the note_no. I would like to take each and combine them into
> one string. There can be an unlimited amount of note_no(s) and
> term_no(s).
>
> DESIRED RESULT:
> If the terms_no is 2 then combine the two lines into one string like
> the following:
>
> note_text_combined
> This is a test. This is a test second line.
>
> Any ideas on how I can accomplish this through SQL I would very much
> appreciate it. If I had the choice to redesign this table I would
> without question, however, it can't be done.


In SQL 2000, you need to do this with an iterative solution, although
for some speed, you could accumulate all note_no = 1 at once.

It's not clear where you will save the result, but you use a varchar(8000) -
and hope that you don't run out of space.

If the result for one termns_no can exceed 8000 characters, you need to
do this client-side.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:37 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com