简体   繁体   中英

How to insert a multiple else in a case statement in sql server with different conditions?

I'm really struggling with how to insert an else in my case statement with different conditions...I'm not sure if it's the syntax that is wrong or what?it's not working, I don't have any error messages, the problem is I still have a Null in my alias for some cases...

    UPDATE Table
    SET Alias = SUBSTRING(Names, 10, 40);

    UPDATE Table
            SET Alias = CASE WHEN  Alias LIKE 'core.user%'  OR Alias LIKE  'core.idea%' OR Alias LIKE'core.form%' 
                             THEN STUFF(Alias, CHARINDEX('.', Alias) + 5, 0, '-') 
                             WHEN  Alias LIKE 'core.badge%'  OR Alias LIKE  'core.award%' OR Alias LIKE 'core.field%' OR Alias LIKE 'core.audit%' OR Alias LIKE 'core.event%'
                             THEN STUFF(Alias, CHARINDEX('.', Alias) + 6, 0, '-')
                             ELSE CASE len(Alias) when charindex('.',Alias) +4 then Alias
                                  ELSE CASE len(Alias)when charindex('.',Alias)+ 5 then Alias
                                        END
                                   END
                         END;

     /*the result for this part is:*/
     Names                                 Alias
     idealink.core.userbadge               core.user-badge
     idealink.core.ideacategories          core.idea-categories
     idealink.core.awardtype               core.award-type        /*till here it's working,but when*/
     idealink.core.user                    NULL        ---I'd like to have (core.user)----
     idealink.core.idea                    NULL        ---I'd like to have (core.idea)----
     idealink.core.award                   NULL        ---I'd like to have (core.award)---- 

     /*I think the problem is here, I'm having Null*/

The purpose of all this is get rid of the idealink, and put a dash between the words when the string is too long after the core. ;but in some cases the string is not so long, so I don't need the dash like (core.user,core.idea,core.award) and so on, just get rid of the idealink.

I really appreciate any help of you guys. Thanks

The reason is because core.user and core.userbridge would both fall under the criteria you defined for Alias like 'core.user%'

To explicitly set values for core.user,core.idea,core.award, you would do as follows.

Check the portion between the /****************/

UPDATE Table
    SET Alias = SUBSTRING(Names, 10, 40);

UPDATE Table
   SET Alias = CASE /*****************************************************************************************/
                    WHEN Alias in('core.user','core.idea','core.award')
                    THEN Alias                  
                    /*****************************************************************************************/
                    WHEN Alias LIKE 'core.user%'  OR Alias LIKE  'core.idea%' OR Alias LIKE'core.form%' 
                    THEN STUFF(Alias, CHARINDEX('.', Alias) + 5, 0, '-') 
                    WHEN  Alias LIKE 'core.badge%'  OR Alias LIKE  'core.award%' OR Alias LIKE 'core.field%' OR Alias LIKE 'core.audit%' OR Alias LIKE 'core.event%'
                    THEN STUFF(Alias, CHARINDEX('.', Alias) + 6, 0, '-')
                    ELSE CASE len(Alias) when charindex('.',Alias) +4 then Alias
                         ELSE CASE len(Alias) when charindex('.',Alias)+ 5 then Alias
                               END
                         END
                END;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM