August 16, 2012 at 11:25 pm
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.
August 16, 2012 at 11:32 pm
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 😀
August 16, 2012 at 11:36 pm
hi
i have to solve with above senario and above mention condition is exists world
August 17, 2012 at 12:17 am
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, '-')
--Vadim R.
August 17, 2012 at 12:17 am
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
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
August 17, 2012 at 12:18 am
Create a function or stored procedure with @hyphen and @inputstring as arguments or parameters
~ Lokesh Vij
Link to my Blog Post --> www.SQLPathy.com[/url]
Follow me @Twitter
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply