首页 > 解决方案 > 文本包含两个连字符时的 SQL SERVER 情况

问题描述

我在写案例时遇到了问题。如果一个文本中有两个连字符,我如何在 sql server 语句中写一个 case 表达式?

行值如下:

  1. PA-PB-PC(2 个连字符)

  2. 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

对此有任何想法吗?

标签: sqlsql-serverssms

解决方案


使用模式匹配:

CASE WHEN YourColumn LIKE '%-%-%' THEN [True Expression] ELSE [False Expression] END

推荐阅读