我想做以下查询:
SELECT SUBSTRING(violations, CHARINDEX('X', violations, 0), 5) AS 'violations', definition, value, date
FROM NewViolations WHERE CHARINDEX('X', violations, 0) > 0
但是,我似乎无法弄清楚如何放入CHARINDEX('X', violations, 0)
一个变量 ( VarX
) 中,所以我最终得到了类似的东西:
SELECT SUBSTRING(violations, VarX, 5) AS 'violations', definition, value, date
FROM NewViolations WHERE VarX > 0
我这样做是为了避免两次运行 CHARINDEX。