SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Summing Strings in SQL...
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 05/07/2002 :  11:05:56  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
Ed writes "I continue to find needs for summing strings in SQL. I am basically trying to "Flatten" one-to-manys. For example, if you have a table with 2 columns, 20 rows. Lets say the first column contains the days of the week, monday, tues etc. randomly. Lets say the second column contains names, Joe, Fred, Bill etc. What I want to be able to do is say "select sum(name) from mytable where day='Monday'" and return one row that contains the two columns, one of "monday" and one with "Joe, Fred and John" in it if those happened to be the names that correspnded to those days. I know you can't "sum" strings, but it would make my life a lot simpler. Any Ideas?"

YellowBug
Aged Yak Warrior

United Kingdom
616 Posts

Posted - 05/07/2002 :  12:01:57  Show Profile  Reply with Quote
Have a look at this code:

http://vyaskn.tripod.com/code/cat.txt

It should work with slight modifications.
Go to Top of Page

Nazim
A custom title

United Arab Emirates
1408 Posts

Posted - 05/07/2002 :  12:06:31  Show Profile  Reply with Quote
Check the link http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=14095 rrb has done string concatenation here .


--------------------------------------------------------------
Go to Top of Page

jrjohnson@infoed.org
Starting Member

1 Posts

Posted - 05/09/2002 :  07:31:57  Show Profile  Reply with Quote
quote:

Have a look at this code:

http://vyaskn.tripod.com/code/cat.txt

It should work with slight modifications.



Thanks, this is nice, but I was looking for something I could use in an in-line SQL statement, like "select last_name,first_name, concatenate(select all books from pubs) where last_name <> 'Jones'" where the "concatenate" function does not exisit. The results would hoepfully look like.....
Smith Fred book1, book2, book3
Johnson Ed booka, bookb
Murphy Paul bookc, booka,book1,book3

etc. I know I can do it with a "program" or stored procedure, but in my application it needs to be much more "dynamic". In Oracle I can write functions, and use them in SQL statements. Thanks!

Go to Top of Page

robvolk
Most Valuable Yak

USA
15676 Posts

Posted - 05/09/2002 :  08:30:59  Show Profile  Visit robvolk's Homepage  Reply with Quote
quote:
In Oracle I can write functions, and use them in SQL statements.

You can write functions in SQL Server 2000, and also use them in SQL statements. It's not a feature that's limited to Oracle. I hate to gripe (yeah, I know, you don't believe that!) but if Oracle does what you want, maybe you should just use Oracle instead and not complain about SQL Server.

This link might help too:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9978

But it's just a variation on Rob's link that was posted earlier:

http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=14095

The point is, they all work in SQL Server, and they do what you want done. You could even modify them to work as functions.

Go to Top of Page

Teroman
Posting Yak Master

United Kingdom
115 Posts

Posted - 05/09/2002 :  08:56:43  Show Profile  Send Teroman an AOL message  Reply with Quote
you can do it for one name like this

declare @foo char(1000)
select @foo = isnull(rtrim(@foo)+',', '') + rtrim(BookName)
from Books
where name = 'Smith'
select @foo

dont know how much help that'll be

col

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000