Solved

Trimming A Trailing Comma

Posted on 2011-02-18
2
617 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
Comment Utility
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
Comment Utility
That was perfect.

Thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

12 Experts available now in Live!

Get 1:1 Help Now