Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [HELP] SUM & REPLACE

Author  Topic 

knockyo
Yak Posting Veteran

83 Posts

Posted - 2010-01-19 : 04:02:34
Hi all,

I have a problem for the SUM and REPLACE function, the SQL script will as below,


select DISTINCT
CapitalForecastID
,Description
,BudgetOwnerName
,CapitalForecastCostPerUnit
,' + @SelectColNames + '
SUM(' + @SelectColNames + ') AS TotalCost


for the @SelectColNames parameter will return the result as

[Q1 '10] ,[Q2 '10] ,[Q3 '10] ,[Q4 '10] ,


My Question as below,
(1) How can i SUM all the Quarters as above with the REPLACE symbol (+)

(2) Since from the statement above, i have symbol (') and (,). So i cant write the correct way on that.

Please advice.

Thanks :)

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-19 : 04:06:33
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

knockyo
Yak Posting Veteran

83 Posts

Posted - 2010-01-19 : 04:16:48
any hints from the references URL there?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2010-01-19 : 04:29:11
removed the big annoying picture ads.

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.7 out!
Go to Top of Page

knockyo
Yak Posting Veteran

83 Posts

Posted - 2010-01-19 : 04:31:59
any ideas about the issues?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2010-01-19 : 05:02:50
your question isn't really clear. you're trying to create a delimited string of some sort?

provide DDL, sample DML and expected results from that DML for your problem and you'll get the answer sooner.

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.7 out!
Go to Top of Page
   

- Advertisement -