?
Solved

Trimming A Trailing Comma

Posted on 2011-02-18
2
Medium Priority
?
631 Views
Last Modified: 2012-05-11
I have the following query:
SELECT DISTINCT
	Table4.Name,
	(SELECT DISTINCT
		ltrim(rtrim(Table2.PCost_Center)) + ','
	FROM 
		Table2 
	where 
		Table1.Company_Code = Table2.Company_Code
		and Table1.Job_Number = Table2.Job_Number
		AND Table2.PCost_Center <> ''
	FOR XML PATH('')) AS Cost_Centers,
	(SELECT  DISTINCT
		ltrim(rtrim(Table2.PCode)) + ','
	FROM 
		Table2 
	where 
		Table1.Company_Code = Table2.Company_Code
		and Table1.Job_Number = Table2.Job_Number
		AND Table2.PCode <> ''
		FOR XML PATH('')) AS PCodes,
	Table1.Job_Description,
	Table1.Job_Number,
	Table3.Alpha_Field AS Site_ID
FROM 
	Table1
	LEFT OUTER JOIN Table2
		ON Table1.Company_Code = Table2.Company_Code 
		AND Table1.Job_Number = Table2.Job_Number
	LEFT OUTER JOIN Table3
		ON Table1.Company_Code = Table3.Company_Code 
		AND Table1.Job_Number = Table3.Job_Number
		AND Table3.User_Def_Sequence = '000003'
	LEFT OUTER JOIN Table4
		ON Table1.Company_Code = Table4.Company_Code 
		AND Table1.Customer_Code = Table4.Customer_Code 
WHERE 
	(NOT (Table1.Customer_Code IS NULL OR Table1.Customer_Code = '')) 
	and (Table4.Company_Code = 'ENG' or Table4.Company_Code = 'INC')

Open in new window

The sub queries which produces Cost_Centers and PCodes produces a value that always ends with a comma (unless the result is null).  How can I trim off the trailing comma?
0
Comment
Question by:Clif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 34927520
you could do:
;with data as ( <yourcurrentselect> )
SELECT Name
   , LEFT(COST_CENTERS, LEN(COST_CENTERS)-1) cost_centers
   , LEFT(PCodes, LEN(PCodes)-1) PCodes
   ... etc .
  FROM data

Open in new window

0
 
LVL 10

Author Closing Comment

by:Clif
ID: 34927730
That was perfect.

Thanks
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question