SQL Server - CHARINDEX始终返回0

时间:2010-02-19 10:00:11

标签: sql-server tsql string

我有以下查询:

SELECT 
   CAST([Action] AS NVARCHAR(4000)) AS CastAction, 
   CHARINDEX(CAST([Action] AS NVARCHAR(4000)), N'StatusChange') AS FoundIndex
FROM AuditTrail 
WHERE action LIKE '%StatusChange%'

Action是一个NTEXT字段 - 此查询返回许多行,在操作文本中匹配StatusChange,但返回的charindex始终为零...任何想法 - 我需要能够拆分此字符串以整理一些数据吗?

2 个答案:

答案 0 :(得分:13)

你有CHARINDEX参数错误的方法。

答案 1 :(得分:7)

您正在交换parameters

Searches expression2 for expression1 and returns its starting position if found.

尝试:

CHARINDEX(N'StatusChange', CAST([Action] AS NVARCHAR(4000)))