在T Sql中搜索Word(随空格)

时间:2013-06-05 11:52:30

标签: sql sql-server-2008 tsql

我在SQL Server表Transactions中有一个列A

我在该表中有大约1000行,并且在每行中我都有包含

组合的字符串
 "@transaction --space of 3 characters---1" or "@transaction --space of 3 characters---0" these strings may be even repeated n number of times within the single row.

结果 - 我想检查所有

"@transaction --space of 3 characters---1"  ones how many times repeated.
"@transaction --space of 3 characters---0"  zeros how many times repeated.

我尝试了诸如

之类的查询
Select * from A where transaction like '%@transaction --space of 3 characters---1%
Select * from A where transaction like '%@transaction --space of 3 characters---0%

但是这些不能给我想要的结果,因为单列中可能有更多的上述字符串。

有人可以建议如何分别计算所有这两个字符串的结尾1和0吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT count (*) as [Count], 'Strings with 1' as [Comment]
FROM A 
WHERE transaction like cast (@transaction as varchar(50)) +'???1%'

UNION ALL

SELECT count (*) as [Count], 'Strings with 0'
FROM A 
WHERE transaction like cast (@transaction as varchar(50)) +'???0%'