| |

VerySource

 Forgot password?
 Register
Search
View: 1433|Reply: 10

Use of replace

[Copy link]

2

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-10 21:30:01
| Show all posts |Read mode
There is a field c1 in Table T. The value of c1 is:
<Aes> <Cmd> MAReport </ Cmd> <Preview> false </ Preview </ Aes>

<Cmd> </ Cmd> The value in the middle is not fixed and can be any character. Now I want to put all records
<Cmd> </ Cmd> This paragraph is changed to a <Cmd> self-defined </ Cmd> of a fixed character. How do I solve it if I use replace? Thank you.
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 United States

Post time: 2020-4-5 16:45:02
| Show all posts
Use stuff, not replace
Reply

Use magic Report

0

Threads

100

Posts

53.00

Credits

Newbie

Rank: 1

Credits
53.00

 China

Post time: 2020-4-5 17:15:01
| Show all posts
Intercept with substring ().
Reply

Use magic Report

0

Threads

100

Posts

53.00

Credits

Newbie

Rank: 1

Credits
53.00

 China

Post time: 2020-4-5 22:15:02
| Show all posts
left (c1, charindex ('<Cmd>')-1) + 'self-defined' + right (c1, len (c1) -charindex ('</ Cmd>') + 1)
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-5 23:30:02
| Show all posts
replace ('<Aes> <Cmd> MAReport </ Cmd> <Preview> false </ Preview </ Aes>', 'MAReport', 'example')
Reply

Use magic Report

2

Threads

18

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-4-6 10:30:01
| Show all posts
declare @c varchar (8000)
set @c = '<Aes> <Cmd> MAReport </ Cmd> <Preview> false </ Preview </ Aes>'

select @ c = left (@ c, charindex ('<cmd>', @ c) +4) + 'String to be replaced' + right (@ c, len (@c) -charindex ('</ cmd> ', @ c) +1)
print @c
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-4-6 11:15:01
| Show all posts
create table T (c1 varchar (100))
insert T select '<Aes> <Cmd> MAReport </ Cmd> <Preview> false </ Preview </ Aes>'

update T set
c1 = stuff (c1, charindex ('<Cmd>', c1), charindex ('</ Cmd>', c1) -charindex ('<Cmd>', c1) +6, '<Cmd> define it yourself </ Cmd> ')

select * from T
--result
c1
-------------------------------------------------- --------------------------------------------------
<Aes> <Cmd> Define yourself </ Cmd> <Preview> false </ Preview </ Aes>

(1 row (s) affected)
Reply

Use magic Report

0

Threads

35

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-6 11:45:01
| Show all posts
declare @a varchar (8000)
set @a = '<Aes> <Cmd> MAReport </ Cmd> <Preview> false </ Preview </ Aes>'
select left (@ a, charindex ('<Cmd>', @ a) + 4) + 'I' + right (@ a, len (@a) -charindex ('</ Cmd>', @ a) + 1 )
Reply

Use magic Report

2

Threads

18

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-4-6 14:30:01
| Show all posts
update t1 set c = left (c, charindex ('<cmd>', c) +4) + 'String to be replaced' + right (c, len (c) -charindex ('</ cmd>', c ) +1)
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-4-6 15:00:01
| Show all posts
If each row of records contains only one <Cmd> ... </ Cmd>:

declare @ str1 varchar (100), @ str2 varchar (20)
set @ str1 = '<Aes> <Cmd> MAReport </ Cmd> <Preview> false </ Preview> </ Aes>'
set @ str2 = 'testString'

select stuff (@ str1, charindex ('<Cmd>', @ str1) + 5, charindex ('</ Cmd>', @ str1) -charindex ('<Cmd>', @ str1) -5, @ str2)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list