Solved

Trimming A Trailing Comma

Posted on 2011-02-18
2
627 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 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 68
Help with Progress 4gl Rounding Function 6 22
Sql server Error message 3 14
What Is an Error? 2 22
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

740 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