SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SEPARATE WTH HYPHEN TO STRING again


SEPARATE WTH HYPHEN TO STRING again

Author
Message
anuj12paritosh
anuj12paritosh
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 162
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.
sqlsurfing
sqlsurfing
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1034 Visits: 1190
How are you determining where the Hypens will be inserted? Do you have a spec/business rule?

--------------------------------------------------
...0.05 points per day since registration... slowly crawl up to 1 pt per day hopefully :-D
anuj12paritosh
anuj12paritosh
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 162
hi
i have to solve with above senario and above mention condition is exists world
rVadim
rVadim
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1429 Visits: 2308
If you know positions you can use STUFF function:



SELECT STUFF('AD675498IJU76', 5, 0, '-')
SELECT STUFF(STUFF('AD675498IJU76', 5, 0, '-'), 8, 0, '-')
SELECT STUFF(STUFF('AD675498IJU76', 5, 0, '-'), 9, 0, '-')



Lokesh Vij
Lokesh Vij
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3346 Visits: 1599
DECLARE @hyphen       VARCHAR(100) = '2,10',
@inputstring VARCHAR(max) = 'ABCDEFGHIJKLMNOPQRST',
@hyphen_count INT,
@i INT=1,
@key INT;

SET @hyphen_count = LEN(@hyphen) - ( LEN(REPLACE(@hyphen, ',', '')) ) + 1

WHILE @i <= @hyphen_count
BEGIN
IF ( CHARINDEX(',', @hyphen, 1) ) <> 0
SET @key = SUBSTRING(@hyphen, 1, CHARINDEX(',', @hyphen, 1) - 1)
ELSE
SET @key = @hyphen

SET @inputstring = STUFF(@inputstring, @key + @i, 1, '-')
SET @hyphen= SUBSTRING(@hyphen, @key + 1, LEN(@hyphen))
SET @i=@i + 1
END

SELECT @inputstring



~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Lokesh Vij
Lokesh Vij
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3346 Visits: 1599
Create a function or stored procedure with @hyphen and @inputstring as arguments or parameters

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search