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
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 SQL 2005 Query To Remove * From Some Accounts

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.

test
test1*
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 thanks

Bob

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-05-27 : 06:04:26
[code]
select 'test' as a
into #tempa
union select 'test1*'
union select 'test2**'
union select 'test3********'

update t
set a = replace(a,'*','')
from #tempa t

select * from #tempa
[/code]
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-27 : 06:18:13
quote:
Originally posted by RickD


select 'test' as a
into #tempa
union select 'test1*'
union select 'test2**'
union select 'test3********'
union select '*test*4***'
update t
set a = replace(a,'*','')
from #tempa t

select * 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.
Go to Top of Page

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..
Go to Top of Page

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?
Go to Top of Page

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.
Go to Top of Page

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 a
into #tempa
union select 'test1*'
union select 'test2**'
union select 'test3********'
union select '*test*4****'
union select 'test*5'

update #tempa
set a = replace(replace(rtrim(replace(replace(a,' ','#~@'),'*',' ')),' ','*'),'#~@',' ')
where a like '%*'

select * from #tempa
drop table #tempa



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-27 : 11:52:47
Another method


select a,reverse(substring(reverse(a),patindex('%[^*]%',reverse(a)),LEN(a))) as new_a from #tempa
where a like '%*'

or


select a,reverse(substring(a,patindex('%[^*]%',a),LEN(a))) as new_a from
(
select reverse(a) as a from #tempa
) as t
where a like '*%'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-27 : 13:02:01
quote:
Originally posted by madhivanan

Another method


select a,reverse(substring(reverse(a),patindex('%[^*]%',reverse(a)),LEN(a))) as new_a from #tempa
where a like '%*'

or


select a,reverse(substring(a,patindex('%[^*]%',a),LEN(a))) as new_a from
(
select reverse(a) as a from #tempa
) as t
where a like '*%'

Madhivanan

Failing 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.
Go to Top of Page
   

- Advertisement -