Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SEPARATE WTH HYPHEN TO STRING again Expand / Collapse
Author
Message
Posted Thursday, August 16, 2012 11:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:31 AM
Points: 11, Visits: 155
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.
Post #1346334
Posted Thursday, August 16, 2012 11:32 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 4:50 PM
Points: 115, Visits: 824
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
Post #1346335
Posted Thursday, August 16, 2012 11:36 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:31 AM
Points: 11, Visits: 155
hi
i have to solve with above senario and above mention condition is exists world
Post #1346336
Posted Friday, August 17, 2012 12:17 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 1, 2014 2:42 PM
Points: 990, Visits: 2,218
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, '-')


Post #1346342
Posted Friday, August 17, 2012 12:17 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
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

Post #1346343
Posted Friday, August 17, 2012 12:18 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 6:38 PM
Points: 1,371, Visits: 1,560
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

Post #1346344
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse