SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sepratestring
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnpsql
Posting Yak Master

India
241 Posts

Posted - 08/15/2012 :  22:57:07  Show Profile  Reply with Quote

i have string of 20 chars like 'sahshajhsjha3232323sas'

and a table with a column sepwords

id sepratorpos
1 4

when the value of sepratorpos is 4 then string should be : 'sahs-hajhsjha3232323sas'


id sepratorpos
1 6
when the value of sepratorpos is 6 then string should be : 'sahsha-jhsjha3232323sas'



id sepratorpos
1 4,6
when the value of sepratorpos is 6 then string should be : 'sahs-ha-jhsjha3232323sas'



id sepratorpos
1 4,6,8
when the value of sepratorpos is 6 then string should be : 'sahs-ha-jh-sjha3232323sas'

id sepratorpos
1 4,6,8,15
when the value of sepratorpos is 6 then string should be : 'sahs-ha-jh-sjha323-2323sas'

and so on till 20


please help

challenge everything

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/15/2012 :  23:09:34  Show Profile  Reply with Quote
is there a rule for string value ? or a pattern?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pnpsql
Posting Yak Master

India
241 Posts

Posted - 08/15/2012 :  23:11:34  Show Profile  Reply with Quote
a - need to inserted in string accorfing to postions defined in sepratorpos column.

please suggest

challenge everything
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 08/15/2012 :  23:24:45  Show Profile  Reply with Quote
write a UDF to do it.

declare	@string		varchar(20) = 'abcdefghijklmnopqrstuvwxyz',
	@sepratorpos	varchar(10) = '4,6,15'

declare	@newstr	varchar(100)
select	@newstr = isnull(@newstr, '') + ch
from
(
	select	v.number, ch = substring(@string, v.number, 1)
	from	master.dbo.spt_values v
	where	v.type	= 'P'
	and	v.number between 1 and len(@string)
	
	union all
	
	select	numberval, '-'
	from	dbo.CSVTable(@sepratorpos)
) a
order by number, ch

select	@newstr



CSVTable is from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000