| Author |
Topic |
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2007-12-08 : 14:33:13
|
| I wish to return a list of selected integers as a single string. So instead of getting 243648I get 24/36/48.My select query:SELECT CONVERT (varchar(10), quantity) + '/' FROM flexing_stock_transactions WHERE item = 'CH' AND week = 35 GROUP BY quantity ORDER BY quantityreturns24/36/48/How can I achieve my goal please? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-08 : 14:42:01
|
Using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2007-12-08 : 14:45:06
|
Ooops, sorry I should have stated it was SQL Server 2005 in my initial post. |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2007-12-08 : 15:56:03
|
| OK, I figured it out. My solution:SELECT retail, supplier, count = dbo.fn_concat_boxes('CH', 35), totalFROM flexing_stock_transactionsWHERE (item = 'CH' AND week = 35 AND firm_or_commission = 'C')GROUP BY retail, boxes, supplier, totalORDER BY retail DESC Using function:set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[fn_concat_boxes](@item varchar, @week int)RETURNS VARCHAR(100)ASBEGINDECLARE @Output varchar(100)SELECT @Output = COALESCE(@Output + '/', '') + CAST(quantity AS varchar(5))FROM flexing_stock_transactionsWHERE item = @item AND week = @weekGROUP BY quantityORDER BY quantityRETURN @Output END |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-12-08 : 22:44:11
|
using FOR XML PATH is generally faster in doing the concat than this method. elsasoft.org |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2007-12-09 : 06:12:52
|
| I see. Any chance of an example please? I am a SQL newbie and have no knowledge of this. |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-12-09 : 12:35:23
|
http://weblogs.sqlteam.com/mladenp/archive/2007/06/01/60220.aspx elsasoft.org |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-12-09 : 15:18:59
|
| >>using FOR XML PATH is generally faster in doing the concat than this method.Is it? Has anyone tested the performance?- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2007-12-10 : 08:23:47
|
| Many thanks guys. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-12-10 : 09:36:44
|
| Good links, thanks guys. Of course repeated ISNULL/COALSESCE/CASE calls will not be as fast as possible, the fastest/best technique is to simply concate and then return only a substring. Doesn't madhivanan's link confirm this? Or am I missing something? It appears to be faster to use a UDF rather than than XML if you do it efficiently. - Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
|