Results 1 to 10 of 10

Thread: SQL Replace Function

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    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!

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    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



  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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 '' ?

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    Re: SQL Replace Function

    So, in essence, I want this.

    REPLACE(Field, NOT IN ('AB', 'AC', ''), '')

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    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.

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  10. #10
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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
  •  



Click Here to Expand Forum to Full Width