Author |
Topic |
bobbles22
Starting Member
18 Posts |
Posted - 2010-05-27 : 05:57:28
|
I have a problem with an accounts db. As a hang over from older versions of the data, some account names have a number of * at the end of the name (different numbers of *'s meant different things). e.g.testtest1*test2**test3********I need a query to go through and for those accounts with these extra characters at the end, remove them. Could someone tell me the code needed to select only those accounts with these *'s at the end, the how to remove only that (possibly using the left command I think)? Many thanksBob |
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2010-05-27 : 06:04:26
|
[code]select 'test' as ainto #tempaunion select 'test1*'union select 'test2**'union select 'test3********'update tset a = replace(a,'*','')from #tempa tselect * from #tempa[/code] |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-27 : 06:18:13
|
quote: Originally posted by RickD
select 'test' as ainto #tempaunion select 'test1*'union select 'test2**'union select 'test3********'union select '*test*4***'update tset a = replace(a,'*','')from #tempa tselect * from #tempa
That will not only replace the unwanted *'s at the end! No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2010-05-27 : 06:27:40
|
Hey, I can only work with the data I am given.. |
|
|
bobbles22
Starting Member
18 Posts |
Posted - 2010-05-27 : 06:42:49
|
Thanks for that guys. This looks like it copies the data to another table. Is there a way of doing it with the data in-situ like using a if or where command? |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2010-05-27 : 06:46:57
|
What do you mean, 'This looks like it copies the data to another table' ? The only reason I have put the data into a table is because I assume you have it in a table. Replace #tempa with your tablename and a with your fieldname. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-27 : 07:58:33
|
This deletes the asterixes only at the end of the column.You need only the update statement and there replace #tempa by the name of your table and column name a by your column name.The rest of the code is only to have testdata...select 'test' as ainto #tempaunion select 'test1*'union select 'test2**'union select 'test3********'union select '*test*4****'union select 'test*5'update #tempaset a = replace(replace(rtrim(replace(replace(a,' ','#~@'),'*',' ')),' ','*'),'#~@',' ')where a like '%*'select * from #tempadrop table #tempa No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-27 : 11:52:47
|
Another methodselect a,reverse(substring(reverse(a),patindex('%[^*]%',reverse(a)),LEN(a))) as new_a from #tempa where a like '%*'orselect a,reverse(substring(a,patindex('%[^*]%',a),LEN(a))) as new_a from (select reverse(a) as a from #tempa ) as twhere a like '*%'MadhivananFailing to plan is Planning to fail |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-27 : 13:02:01
|
quote: Originally posted by madhivanan Another methodselect a,reverse(substring(reverse(a),patindex('%[^*]%',reverse(a)),LEN(a))) as new_a from #tempa where a like '%*'orselect a,reverse(substring(a,patindex('%[^*]%',a),LEN(a))) as new_a from (select reverse(a) as a from #tempa ) as twhere a like '*%'MadhivananFailing to plan is Planning to fail
Ah! patindex() !I have tried that with charindex() and that would not work.So I have used another solution. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
|