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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add minus every 5 characters 1

Status
Not open for further replies.

peac3

Technical User
Jan 17, 2009
226
AU
Hi Guys,

I am trying to update column to add minus in every 5 characters inside the value using T-SQL.

eg the original value is 1234567890123456789012, I would like to update it become 12345-67890-12345-67890-12.

any advise would be much appreciated.

Thanks,
 
I'd be looking at the "STUFF" command here.
If you can't be sure of how long the string is, it may be easier to loop for the length of the string - perhaps make it a function to call?

example:
Code:
DECLARE @string as varchar(max), @loop as int
SET @string = '1234567890123456789'
SET @loop = LEN(@string)/5
WHILE @loop > 0
BEGIN
SET @string = STUFF(@string,1+(@loop*5),0,'-')
SET @loop = @loop-1
END
SELECT @string

soi là, soi carré
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top