-1

I have to make a difficult word processing. How can I change dynamically as the following example?

Example: /hello/ baby /deneme/ /hello2/

Output: (/hello/) baby (/deneme/) (/hello2/)

user2416205
  • 13
  • 1
  • 7

1 Answers1

0

This is a pretty rudimentary solution, but it works for the case you've given (SQL Fiddle here):

SELECT
  in_str,
  (
    -- If the string starts with '/', prepend '('
    CASE WHEN in_str LIKE '/%' THEN '(' ELSE '' END
    -- Replace / after a space with (/
    + REPLACE(
        -- Replace / before by a space with /)
        REPLACE( in_str, ' /', ' (/' ),
        '/ ', '/) '
      )
    -- If the string ends with '/', append ')'
    + CASE WHEN in_str LIKE '%/' THEN ')' ELSE '' END
  ) AS out_str
FROM table1;

If table1 has the following in_str values this will give you the corresponding out_str values:

in_str                    out_str
------------------------  ------------------------------
/one/ two /three/ /four/  (/one/) two (/three/) (/four/)
one /two/ /three/         one (/two/) (/three/)
/one/ /two/ three         (/one/) (/two/) three
//one / // two/ /         (//one (/) (//) two/) (/)

I've included the last one to demonstrate some edge cases. Also note that this only handles / characters immediately followed by a space or the beginning or end of the string. Other whitespace characters like newlines and tabs aren't handled. For example, if you had a string like this (where indicates a newline and a tab):

/one/⇒/two/⏎
/three/⏎

...the output you would get is this:

(/one/⇒/two/⏎
/three/⏎

You could handle these scenarios with additional REPLACE functions, but that's a rabbit hole you'll have to jump down yourself.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • thx :( but now work under code Example : [Dealer_Type] in( 'xx' , 'xxx') and [Is_Success] = '1' and [Product_Full_Name] in ( N'xxx' ) Output : [Dealer_Type] in( ['xx'] , ['xxx']) and [Is_Success] = ['1'] and [Product_Full_Name] in ( N['xxx'] ) – user2416205 Mar 18 '14 at 21:09