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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 separate with hyphen to string

Author  Topic 

paritosh
Starting Member

42 Posts

Posted - 2012-08-06 : 07:51:17
HI ALL

I HAVE STRING SUPPOSE

STRING :-
AD675498IJU76

I WANT TO SEPARATE WITH HYPHEN (-)
BUT EVERY TIME POSITION IS NOT FIX.
ASSUME FIRST TIME POSITION IS :- 4
THEN RESULT : -

AD67-5498IJU76

ASSUME NEXT TIME POSITION IS :- 4,6
THEN RESULT : -

AD67-54-98IJU76

ASSUME NEXT TIME POSITION IS :- 4,7
THEN RESULT : -

AD67-549-8IJU76

I WANT TO DYANAMIC QUERY FOR SOLVE THIS PROBLEM

THANKS 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,'-')
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -