Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL-'08: Are multiple Replace statements a bad practice/is there another way to write this query?

Select 
Distinct 
    REPLACE(REPLACE(REPLACE(REPLACE(Category, ' & ', '-'), '/', '-'), ', ', '-'), ' ', '-') AS Department 
From 
     Inv WITH(NOLOCK) 

I was wondering because I am a Jr. ETL Engineer and want to develop good habits.

Obviously this could get even longer in many circumstancials.

like image 218
Troy McLure Avatar asked Mar 29 '11 15:03

Troy McLure


People also ask

How do you do multiple replaces in SQL?

SELECT REPLACE(REPLACE(REPLACE(REPLACE('3*[4+5]/{6-8}', '[', '('), ']', ')'), '{', '('), '}', ')'); We can see that the REPLACE function is nested and it is called multiple times to replace the corresponding string as per the defined positional values within the SQL REPLACE function.

Is there a Replace command in SQL?

SQL Server REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.

Can we use Replace in UPDATE query?

You can use REPLACE in an UPDATE statement.


2 Answers

The nested replace is fine, but as the nesting level increases the readability of your code goes down. If I had a large number of characters to replace I would opt for something cleaner like the below table driven approach.

    declare @Category varchar(25)
    set @Category = 'ABC & DEF/GHI, LMN OP'
    -- nested replace
    select replace(replace(replace(replace(@Category, ' & ', '-'), '/', '-'), ', ', '-'), ' ', '-') as Department 

    -- table driven
    declare @t table (ReplaceThis varchar(10), WithThis varchar(10))
    insert into @t
        values  (' & ', '-'), 
                ('/', '-'),
                (', ', '-'),
                (' ', '-')

    select  @Category = replace(@Category, ReplaceThis, isnull(WithThis, ''))                       
    from    @t
    where   charindex(ReplaceThis, @Category) > 0;

    select @Category [Department]
like image 90
nathan_jr Avatar answered Oct 27 '22 01:10

nathan_jr


You might be better off using the SQLCLR and a regex. http://blogs.msdn.com/b/sqlclr/archive/2005/06/29/regex.aspx

Certainly that can be more maintainable and flexible.

As far as performance, you typically find it hard to beat built-in functions, but with many REPLACE operations, the CLR may outperform it - you'll have to benchmark.

I notice you said you are doing this in SSIS - in that case, you can use a variety of other possible methods within your data flows, including a script task and regex in those. As a general rule, you need to assess each operation you are doing and decide if it should be done in the query which brings data into the data flows or in the data flow itself. Some operations can be better to do (like filtering) on the source, but others (like aggregating), might be better done in the dataflow, especially if they are stateful with any kind of running data.

like image 45
Cade Roux Avatar answered Oct 27 '22 00:10

Cade Roux