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
 sepratestring

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-08-15 : 22:57:07

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

52326 Posts

Posted - 2012-08-15 : 23:09:34
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

246 Posts

Posted - 2012-08-15 : 23:11:34
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)

17689 Posts

Posted - 2012-08-15 : 23:24:45
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -