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
 parse string

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-05-15 : 11:28:55
i need to parse a string like

[p]a]a]s]s][p

when a [ sign occurs then next letter need to be convert in upper case and when a ]sign occurs the
next letter need to be convert in lower case.the sign [and ] need to be removed

the desired output of is : [p]a]a]s]s][p string is PaassP

please help

challenge everything

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-15 : 11:42:23
[code]
declare @str varchar(20) = '[p]a]a]s]s][p'
declare @pos int

select @pos = charindex('[', @str)

while @pos <> 0
begin
select @str = stuff(@str, @pos + 1, 1, upper(substring(@str, @pos + 1, 1)))
select @pos = charindex('[', @str, @pos + 1)
end

select replace(replace(@str, '[', ''), ']', '')
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-15 : 22:26:50
[code]
declare @str varchar(20) = '[p]a]a[s]s][p',@rstr varchar(20)
declare @pos int
;With Num(N)
AS
(
SELECT 1
UNION ALL
SELECT N+1
FROM Num
WHERE N+1 <= LEN(@str)
)
select @rstr=COALESCE(@rstr,'') + CASE WHEN SUBSTRING(@str,N-1,1)='[' THEN UPPER(SUBSTRING(@str,N,1)) WHEN SUBSTRING(@str,N-1,1)=']' THEN LOWER(SUBSTRING(@str,N,1)) ELSE SUBSTRING(@str,N,1) END
from Num
WHERE N >1
AND SUBSTRING(@str,N,1) NOT IN ('[',']')

select @rstr
[/code]

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

Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-05-20 : 23:35:30
resolved... thanks


challenge everything
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-21 : 16:19:53
welcome

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

Go to Top of Page
   

- Advertisement -