|
-
Dec 2nd, 2008, 09:51 AM
#1
Thread Starter
Addicted Member
SQL Replace Function
I am trying to use the REPLACE function to replace a string. The only problem is, I want it to replace any string besides a certain few.
So I want it to replace anything that is not 'AB', 'AC', or empty string ''. The column can contain many different strings, hardcoding each variation is not feasible, example "REPLACE(Field, 'DD', '')". I'd have to do this for each different variation I find, and I'd rather not do this. Is there a way to have the REPLACE function replace all occurrences not matching "IN ('AB', 'AC', '')"?
Hopefully that makes sense.
Thanks!
-
Dec 2nd, 2008, 10:41 AM
#2
Re: SQL Replace Function
The replace function is a VB function not SQL !!
,anyway replace doesn't work like that you can only replace one item in the string at a time.
If you have to do the replace for a number of different Fields you could just loop through them. but if it is the same field and you want to do several replaces on it, then as far as I am aware you would have to write the Replace code for each one.
Please Mark your Thread "Resolved",  if the query is solved & Rate those who have helped you
-
Dec 2nd, 2008, 10:48 AM
#3
Thread Starter
Addicted Member
Re: SQL Replace Function
Thanks for the response. REPLACE is also a SQL function. I'm in a query window so I know it is, I'm not even touching the VB side of things. This question is purely SQL.
Would there be another SQL function that would accomplish what I'm after?
Thanks
-
Dec 2nd, 2008, 11:40 AM
#4
Re: SQL Replace Function
It is not a standard SQL function, it is specific to whatever implementation of SQL you are using - which is why people usually ask (as does the "Before you post" thread at the top of this forum) for you to always tell us which database system you are using.
I'm also a bit unclear on what you are trying to achieve.. do you mean that if the field value is 'AB' or 'AC' then leave it alone, otherwise change it to '' ?
-
Dec 2nd, 2008, 11:42 AM
#5
Re: SQL Replace Function
Can you give us a sample of your data?
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Dec 2nd, 2008, 11:46 AM
#6
Thread Starter
Addicted Member
Re: SQL Replace Function
Sorry, I am using SQL Server 2005, forgot to include that.
Basically what I want is to replace anything that isn't 'AB', 'AC', or '' to an empty string ''. I am wanting to ignore all other values.
-
Dec 2nd, 2008, 11:47 AM
#7
Thread Starter
Addicted Member
Re: SQL Replace Function
So, in essence, I want this.
REPLACE(Field, NOT IN ('AB', 'AC', ''), '')
-
Dec 2nd, 2008, 11:50 AM
#8
Thread Starter
Addicted Member
Re: SQL Replace Function
Just to clear up my response, I do not want to ignore them, I want to replace them with ''. This is required so the join will return the expected results. I'm confusing myself now.
-
Dec 2nd, 2008, 11:50 AM
#9
Re: SQL Replace Function
You could use an update to do this:
Code:
declare @table table(field char(2))
insert into @table(field) values ('')
insert into @table(field) values ('AB')
insert into @table(field) values ('AC')
insert into @table(field) values ('AD')
insert into @table(field) values ('DD')
update @table
set field = ''
where field not in ('AB','AC','')
select * from @table
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Dec 2nd, 2008, 11:52 AM
#10
Re: SQL Replace Function
Or, a case statement:
Code:
declare @table table(field char(2))
insert into @table(field) values ('')
insert into @table(field) values ('AB')
insert into @table(field) values ('AC')
insert into @table(field) values ('AD')
insert into @table(field) values ('DD')
select
case
when field = 'AB' then 'AB'
when field = 'AC' then 'AC'
else ''
end
from @table
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|