[英]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...我真的很困惑如何在我的 case 语句中插入一个具有不同条件的 else ......我不确定是语法错误还是什么?它不起作用,我没有任何错误消息,问题是在某些情况下我的别名中仍然有一个 Null ......
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.所有这一切的目的是摆脱idealink,当核心后字符串太长时,在单词之间放置一个破折号。 ;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.
;但在某些情况下,字符串并没有那么长,所以我不需要像 (core.user,core.idea,core.award) 之类的破折号,只需去掉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%'原因是 core.user 和 core.userbridge 都属于您为 Alias 定义的标准,例如“core.user%”
To explicitly set values for core.user,core.idea,core.award, you would do as follows.要显式设置 core.user,core.idea,core.award 的值,您可以执行以下操作。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.