Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
HI ALLI HAVE STRING SUPPOSESTRING :-AD675498IJU76I WANT TO SEPARATE WITH HYPHEN (-)BUT EVERY TIME POSITION IS NOT FIX.ASSUME FIRST TIME POSITION IS :- 4THEN RESULT : -AD67-5498IJU76ASSUME NEXT TIME POSITION IS :- 4,6THEN RESULT : -AD67-54-98IJU76ASSUME NEXT TIME POSITION IS :- 4,7THEN RESULT : -AD67-549-8IJU76I WANT TO DYANAMIC QUERY FOR SOLVE THIS PROBLEMTHANKS IN ADVANCE.
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-08-06 : 07:57:59
You can do this using STUFF function - see the example below. The location where you want to insert the hyphen is in @POSITION.
DECLARE @X VARCHAR(32) = 'AD675498IJU76'DECLARE @POSITION INT = 4;SELECT STUFF(@x,@POSITION+1,0,'-')
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-08-06 : 09:44:05
is the number of _ to be static? ie always 4,5 etc or does it depend on overall string length?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/