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
 General SQL Server Forums
 New to SQL Server Programming
 Does SQL have a way of exluding a final comma?

Author  Topic 

stoolpidgeon
Starting Member

28 Posts

Posted - 2013-07-25 : 11:44:56
The question may seem a bit vague but the following should help to demonstrate my problem:

declare @maxMonth integer
declare @month integer

set @month = 1
set @maxMonth = (select max(month(importdate)) from tblsales)

select
while @month < @maxMonth
begin
(case month(importdate) when @month then invoicedvalue end) as (select cast(importdate as char(3)) where month(importdate) = @month),
@month = @month + 1
end
from tblsales
where importdate > '01/01/2013'

Can't see why there would be an issue with this script except that if I loop on the case statement I end up with a final comma which I obviously don't want!

Is there some way around this?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-25 : 11:55:06
quote:
Originally posted by stoolpidgeon

The question may seem a bit vague but the following should help to demonstrate my problem:

declare @maxMonth integer
declare @month integer

set @month = 1
set @maxMonth = (select max(month(importdate)) from tblsales)

select
while @month < @maxMonth
begin
(case month(importdate) when @month then invoicedvalue end) as (select cast(importdate as char(3)) where month(importdate) = @month),
@month = @month + 1
end
from tblsales
where importdate > '01/01/2013'

Can't see why there would be an issue with this script except that if I loop on the case statement I end up with a final comma which I obviously don't want!

Is there some way around this?

Are you using Microsoft SQL Server? The syntax (even pseudo syntax) doesn't seem like it is T-SQL compliant.

Regardless, one way to remove trailing comma would be to use a left function on your final output:
LEFT(YourFinalOutput,LEN(YourFinalOutput)-1)


If you are trying to concatenate values in a column of a table, there are faster and better approaches - here are couple of examples:

http://sqlandme.com/2011/04/27/tsql-concatenate-rows-using-for-xml-path/
http://myshallowsqlblog.wordpress.com/the-lowdown-on-that-xml-path-thingie/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-26 : 00:11:54
The code looks like you attempt is to do dynamic pivoting based on monthnames to me


..
(case month(importdate) when @month then invoicedvalue end) as (select cast(importdate as char(3)) where month(importdate) = @month),
..


If yes, you dont need a loop
have a look at this approach

http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -