我在写案例时遇到问题。如果一个文本中有两个连字符,如何在sql server语句中写为大小写表达式?
行值如下:
PA-PB-PC(两个连字符)
PA-PAC(1个连字符)
所需的结果将是这样
Case when (section text contains two hyphens) then isnull(parsename(replace
(ResponsibleSection,'-','.'),2) else isnull(parsename(replace
(ResponsibleSection,'-','.'),1)
这是我原来的问题代码:-
select ResponsibleSection = isnull(parsename(replace
(ResponsibleSection,'-','.'),2),Section) from FeedbackOwnerSetting
Group by ResponsibleSection
对此有任何想法吗?
答案 0 :(得分:3)
使用模式匹配:
CASE WHEN YourColumn LIKE '%-%-%' THEN [True Expression] ELSE [False Expression] END
答案 1 :(得分:2)
而不是已经提出模式匹配,我建议只计算连字符的出现次数>
CASE LEN(YourColumn) - LEN(REPLACE(YourColumn, '-', '')) WHEN 2 THEN [True Expression] ELSE [False Expression] END
答案 2 :(得分:2)
使用replace()删除“-”并找出长度上的差异
CASE len(col) - len(replace(col, '-', ''))
WHEN 1 THEN '1 hyphens'
WHEN 2 THEN '2 hyphens'
END