Solved

Trimming A Trailing Comma

Posted on 2011-02-18
2
620 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
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now