Search code examples
sqlreplacenestedcase-sensitive

How do you perform a CASE SENSITIVE nested REPLACE command?


I have a table:

Building                                        Building
1__bEast                                        1 East
1__bSouth                                       1 South
500__bBldg__d                                   500 Bldg.
501__bBldg__d                                   501 Bldg.
B__u1                                           B-1
B__u2                                           B-2
B__u2__bWest                                    B-2 West
Building__b10__b__PBldg__bTen__p                Building 10 (Bldg Ten)
D__7T__b__PDiagnostic__b__7__bTreatment__p      D & T (Diagnostic & Treatment)
n__fa                                           n/a

The list on the left shows the actual table. The list on the right is how I want them to appear in the results. In order for me to do this, I am using the following nested REPLACE command:

REPLACE(
    REPLACE(
        REPLACE(
            REPLACE(
                REPLACE(
                    REPLACE(Building,'__P','(')
                ,'__p',')')
            ,'__b',' ')
        ,'__u','-')
    ,'__7','&')
,'__d','.') as [Building]

The problem with this is that the "__P" and "__p" are treated the same and the results end up like this:

Building 10 (Bldg Ten(
D & T (Diagnostic & Treatment(

The right parentheses ")" do not show up.

I tried using:

...REPLACE(REPLACE(Building COLLATE SQL_Latin1_General_CP1_CI_AS,'__P','('),'__p',')')...

but it doesn't seem to work in a nested REPLACE query. I get basically the same results.

Is there a way to do this?


Solution

  • Use COLLATE SQL_Latin1_General_CP1_CS_AS (case sensitive):

    CREATE TABLE #tab(Building VARCHAR(100));
    
    INSERT INTO #tab
    SELECT '1__bEast'                                     
    UNION ALL SELECT '1__bSouth'                                       
    UNION ALL SELECT '500__bBldg__d'                                  
    UNION ALL SELECT '501__bBldg__d'                                   
    UNION ALL SELECT 'B__u1'                                           
    UNION ALL SELECT 'B__u2'                                           
    UNION ALL SELECT 'B__u2__bWest'                                    
    UNION ALL SELECT 'Building__b10__b__PBldg__bTen__p'              
    UNION ALL SELECT 'D__7T__b__PDiagnostic__b__7__bTreatment__p'      
    UNION ALL SELECT 'n__fa';
    
    SELECT Building,
    REPLACE(
        REPLACE(
            REPLACE(
                REPLACE(
                    REPLACE(
                        REPLACE(Building COLLATE SQL_Latin1_General_CP1_CS_AS ,'__P','(')
                    ,'__p',')')
                ,'__b',' ')
            ,'__u','-')
        ,'__7','&')
    ,'__d','.') as [Building] 
    FROM #tab
    

    LiveDemo

    Output:

    ╔════════════════════════════════════════════╦══════════════════════════════╗
    ║                  Buidling                  ║           Replaced           ║
    ╠════════════════════════════════════════════╬══════════════════════════════╣
    ║ 1__bEast                                   ║ 1 East                       ║
    ║ 1__bSouth                                  ║ 1 South                      ║
    ║ 500__bBldg__d                              ║ 500 Bldg.                    ║
    ║ 501__bBldg__d                              ║ 501 Bldg.                    ║
    ║ B__u1                                      ║ B-1                          ║
    ║ B__u2                                      ║ B-2                          ║
    ║ B__u2__bWest                               ║ B-2 West                     ║
    ║ Building__b10__b__PBldg__bTen__p           ║ Building 10 (Bldg Ten)       ║
    ║ D__7T__b__PDiagnostic__b__7__bTreatment__p ║ D&T (Diagnostic & Treatment) ║
    ║ n__fa                                      ║ n__fa                        ║
    ╚════════════════════════════════════════════╩══════════════════════════════╝
    

    Add also one more REPLACE(..., 'n__fa', 'n/a') or REPLACE(..., '__f', '/') depending on your needs.