Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add semicolon every 7th place

Status
Not open for further replies.

captainpicart

Programmer
Mar 17, 2011
4
Hi experts,

How to write a stored procedure which will get rid of all semicolons at the beginning and at the end, BUT will separate each number with a semi colon?


Input:---
018752;018752;019912;002541;017181;;;;
;018752;018752;018752;019536;020921;007378;013099;;;;;;;;
;;;018752;018752;018752;029437;017554;027857;031346;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;018752;018752;018752;029437;017554;027732;011219;;;
;018752;018752;018752;019536;020921;007873;
;018752;018752;018752;019536;020921;007873

Output:-
018752;018752;019912;002541;017181
018752;018752;018752;019536;020921;007378;013099
018752;018752;018752;029437;017554;027857;031346
018752;018752;018752;029437;017554;027732;011219
018752;018752;018752;019536;020921;007873
018752;018752;018752;019536;020921;007873

Note: The numbers will always be 6 digits in length.

Please Help!

Thank you!

 
There's a neat little trick you can use.

1. Replace Semi-colons with spaces
2. Left Trim spaces
3. Right Trim spaces
4. Replace spaces with semi-colons.

Ex:

Code:
Declare @Temp Table(Data VarChar(100))

Insert Into @Temp Values('018752;018752;019912;002541;017181;;;;')
Insert Into @Temp Values(';018752;018752;018752;019536;020921;007378;013099;;;;;;;;')
Insert Into @Temp Values(';;;018752;018752;018752;029437;017554;027857;031346;;;;;;;;;;;;;;;;;;;;;;;;;;;')
Insert Into @Temp Values(';;018752;018752;018752;029437;017554;027732;011219;;;')
Insert Into @Temp Values(';018752;018752;018752;019536;020921;007873;')
Insert Into @Temp Values(';018752;018752;018752;019536;020921;007873')

Select Data, Replace(LTrim(RTrim(Replace(Data, ';', ' '))), ' ',';')
From   @Temp


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
You're welcome.


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top