Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何查找SQL Server 2008列中是否只有一个连字符?_Sql_Sql Server 2008_Sql Server 2012 - Fatal编程技术网

如何查找SQL Server 2008列中是否只有一个连字符?

如何查找SQL Server 2008列中是否只有一个连字符?,sql,sql-server-2008,sql-server-2012,Sql,Sql Server 2008,Sql Server 2012,假设我有一个表“customer”,如下所示: +--------------------------------------------------+--------+ | Customer | ID | +--------------------------------------------------+--------+ | OSHKOSH CORPORATION-17180

假设我有一个表“
customer
”,如下所示:

+--------------------------------------------------+--------+
|                     Customer                     |   ID   |
+--------------------------------------------------+--------+
| OSHKOSH CORPORATION-17180                        | cust12 |
| L&T-IES-P&G KABUSHIKI KAISHA-4216                | cust13 |
| THE PROCTER-GAMBLE MANUFACTURING COMPANY-17214   | cust14 |
+--------------------------------------------------+--------+
我应该只得到一行,因为列customer只有一个连字符

| OSHKOSH CORPORATION-17180                        | cust12 |

在不考虑性能的情况下,下面的sql可以实现这一点

select *
from customer
where (len(Customer) - len(replace(Customer, '-', ''))) = 1

第一次检查至少有一个
-
,第二次检查至少有两个。组合起来就是一个。

这没有直接的功能,但是你可以用替换和len来完成。类似的问题如下:
select *
from customer
where customer like '%-%'
and customer not like '%-%-%'